0

I've got a Sqlite database which I populate inside a Windows Store app. Now I'd like to have said database availiable when reinstalling the app, therefore I have to add it to my project as a file, if I'm right.

Now I'm trying to export the database file using the PickSaveFileAsync()-Method. Unfortunately I don't really know how to go about this once I've got the file. Here's what I've got so far:

    private async void exportDB_Click(object sender, RoutedEventArgs e)
    {
        StorageFile database = await ApplicationData.Current.LocalFolder.GetFileAsync("Hashes.db");
        var picker = new FileSavePicker();
        picker.FileTypeChoices.Add("Database File", new string[] { ".db" });
        picker.CommitButtonText = "Save DB";
        picker.SuggestedFileName = "Database";
        StorageFile file = await picker.PickSaveFileAsync();
        if (file != null)
        {
            // What to do here?
        }
    }
Thomas
  • 4,030
  • 4
  • 40
  • 79
  • users should not have to export and therefore import app database between updates or re-installations don't you think? Do you have other user friendly option? – letiagoalves Mar 09 '13 at 13:30
  • There will be an end-user and an admin version for this app, the database export will be only availiable for the latter one. – Thomas Mar 11 '13 at 17:00

1 Answers1

2

I'm having a hard time figuring out what exactly you are trying to achieve.

Looking at the code it seems that you want to copy your database file to a user defined location. To do this just add the following code inside your if block:

using (var srcStream = await database.OpenStreamForReadAsync())
{
    using (var destStream = await file.OpenStreamForWriteAsync())
    {
        await srcStream.CopyToAsync(destStream);
    }
}

Reading the introductory text it seems you're trying to include the file with your application. In this case you should indeed add it as Content to your project. You can then access it from code as follows:

var dbFile = Package.Current.InstalledLocation.GetFileAsync(@"db\Hashes.db");

In case you didn't know, you can find the files in LocalFolder in the following directory:

C:\Users\Username\AppData\Local\Packages\PackageName\LocalState
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Thanks so much, that was indeed exactely what I was looking for. I also didn't know that I can find the database-file in the app folder, that will also come in handy! – Thomas Mar 11 '13 at 17:04