Deploy SQLite to UWP in Xamarin Forms app

RussellFustinoRussellFustino USUniversity ✭✭
edited December 2016 in Xamarin.Forms

I’ve been using the excellent blog to solve my SQLite deployment challenge, and the Android works great from @RobGibbens .

http://arteksoftware.com/deploying-a-database-file-with-a-xamarin-forms-app/

Will be testing iOS as soon ad xcode 8.2 finishes downloading. :)

The article has wp8, but it uses Application.GetResourceStream and that is only for Windows Phone, is there an updated version for UWP?

I think I am close with the code below… but it is blowing up on the BinaryWritter in bold and italic below.

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Trouble shooting tips
Make sure it’s not readOnly, and after checking the file properties right after the CreatFileAsync, it's not
Make sure you have sufficient privileges to access this resource, I do

Any ideas? This code is in the UWP head project

        if (!storageFile.FileExists(dbPath))
        {
            // Open file in application package
            var fileToRead = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///crosswalk.db3", UriKind.Absolute));
            // Create or overwrite file target file in local app data folder
            var fileToWrite = await ApplicationData.Current.LocalFolder.CreateFileAsync("crosswalk.db3", CreationCollisionOption.ReplaceExisting);
            byte[] buffer = new byte[4096];
            // line below blows up
         **_   using (BinaryWriter fileWriter = new BinaryWriter(await fileToWrite.OpenStreamForWriteAsync()))_**
            {
                using (BinaryReader fileReader = new BinaryReader(await fileToRead.OpenStreamForReadAsync()))
                {
                    long readCount = 0;
                    while (readCount < fileReader.BaseStream.Length)
                    {
                        int read = fileReader.Read(buffer, 0, buffer.Length);
                        readCount += read;
                        fileWriter.Write(buffer, 0, read);
                    }
                }
            }
        }

Best Answer

  • RussellFustinoRussellFustino US ✭✭
    edited December 2016 Accepted Answer

    Here are is the magic code for UWP file copy. I found it in the recently posted UWP samples at: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/FileAccess

    Also, as an update, I tested the iOS code and it works fine too.

    Special thanks to @DanHermes for pointing out the resources to me.

           var storageFile = IsolatedStorageFile.GetUserStoreForApplication();
    
    
            if (!storageFile.FileExists(dbPath))
            {
                  // copy storage file; replace if exists.
               var fileToRead = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///crosswalk.db3", UriKind.Absolute));
               Windows.Storage.StorageFolder storageFolder =
                            Windows.Storage.ApplicationData.Current.LocalFolder;
               StorageFile fileCopy = await fileToRead.CopyAsync(storageFolder, "crosswalk.db3", NameCollisionOption.ReplaceExisting);
            }
    

Answers

  • RussellFustinoRussellFustino USUniversity ✭✭
    edited December 2016 Accepted Answer

    Here are is the magic code for UWP file copy. I found it in the recently posted UWP samples at: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/FileAccess

    Also, as an update, I tested the iOS code and it works fine too.

    Special thanks to @DanHermes for pointing out the resources to me.

           var storageFile = IsolatedStorageFile.GetUserStoreForApplication();
    
    
            if (!storageFile.FileExists(dbPath))
            {
                  // copy storage file; replace if exists.
               var fileToRead = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///crosswalk.db3", UriKind.Absolute));
               Windows.Storage.StorageFolder storageFolder =
                            Windows.Storage.ApplicationData.Current.LocalFolder;
               StorageFile fileCopy = await fileToRead.CopyAsync(storageFolder, "crosswalk.db3", NameCollisionOption.ReplaceExisting);
            }
    
Sign In or Register to comment.