dimanche 6 mars 2016

How to correct a System.IO.FileNotFoundException to DB?

I'm connecting to an existing SQLite database in a Windows Phone 8.1 solution. But during the copy of the database to the local storage I get a "System.IO.FileNotFoundException". I also get an SQLite exception which further points to an issue with the file path.

"Could not open database file: C:\Data\Users\DefApps\APPDATA\Local\Packages\6d00c25c-39d2-443f-a29b-2c30c8ce7e99_gevy8cezwa384\LocalState\Databases\ParkingZoneDatabase.db (CannotOpen)"

I tried debugging this further by using two approaches shown below but still get the file not found exception. I also tried editing the path to "ParkingZoneDatabase" but no luck with that path either.

Question - Does anyone know why the file path is not found in this case?

Code - (This is the code that gets the db from the package and copes it to local storage on the device)

      public const string AppDBPath = @"Databases\ParkingZoneDatabase.db";
        public const string PackageDBPath = @"Databases\ParkingZoneDatabase.db";


        public async Task<bool> Init()
        {

            bool isDatabaseExisting = false;

            try
            {
                //The assignment skips here as the file hasn't been copied yet
                StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(AppDBPath);
                isDatabaseExisting = true;
            }
            catch
            {
                isDatabaseExisting = false;
            }

            if (!isDatabaseExisting)
            {


                //Approach #1 - copying file from the package
                StorageFile seedFile = await StorageFile.GetFileFromPathAsync(
        Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
        PackageDBPath));

                //Approach #2 - copying file from the package
                StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(PackageDBPath);

               //This line never runs as the above as the copies above fail.
               await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
            }

            return true;          
        }

The database file is set as content and copy always:

enter image description here

Aucun commentaire:

Enregistrer un commentaire