locked
Problem accessing SQLite database: Access Denied

    Question

  • The following works in a sample app from the web, but not when placed in app created with the Store Hub template, Windows 8.1:

    string fileName = "Northwind.sl3";        //  "Chinook_Sqlite.sqlite"; 
                // build the local path where we expect the database to be. if the db does not exist there yet, copy it from the package
                string localPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName);
                // the File class helps to check if a file exists because this function does not exist in WinRT by default
                if (!await File.FileExistsInPath(localPath))
                {
                    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/"+fileName));
                    await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, fileName);
                }
    The var file = await .... statement fails with access denied even though the SQLite file is in the Assets folder within the project, I have permissions to the file, Build Action = Content, Do Not Copy.

    The following also does not work, fails in about the same place, trying to access the SQLite file in the project directory.

    string fileName = "Northwind.sl3";        //  "Chinook_Sqlite.sqlite"; 
                // build the local path where we expect the database to be. if the db does not exist there yet, copy it from the package
                string localPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName);
                // the File class helps to check if a file exists because this function does not exist in WinRT by default
                if (!await File.FileExistsInPath(localPath))
                {
                    StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                    
                    string locPath = installFolder.Path;
                    int pos = locPath.IndexOf("\\bin");
                    if (pos > 0)
                    {
                        locPath = locPath.Substring(0, pos);
                    }
                    string assetsDbPath = Path.Combine(locPath + "\\Assets\\" + fileName);
                    IStorageFile dbFile = null;
                    try
                    {
                        // SQLite.SQLiteConnectionPool.Shared.Reset();
                        dbFile = await StorageFile.GetFileFromPathAsync(assetsDbPath);
                    }
                    catch 
                    {
                    }
    Thanks for your help..


     

    Friday, January 16, 2015 6:51 PM

All replies

  • Here is a good example on how to include a SQLite database with your app

    http://timheuer.com/blog/archive/2012/06/28/seeding-your-metro-style-app-with-sqlite-database.aspx

    Saturday, January 17, 2015 2:01 PM
  • Ken, thanks for your reply.

    I have tried this code and various permutations of it and still get the access denied on the seed file in my application source folder. What puzzles me is it works in a sample app I downloaded from the web, but not when copied into my own test app, Assets folder.

    The Tim Heuer post is over 2.5 years old. I am trying to work with Windows 8.1 and VS 2013. I have referenced the latest SQLite and SQLite-net extensions. I have permissions for the file. Still get access denied on the "dbFile = await StorageFile.GetFileFromPathAsync(assetsDbPath)" statement.

    What am I missing?

    Saturday, January 17, 2015 10:20 PM