0

I want copy file in my app to a folder with FileSavePicker. My Code:

var fileSavePicker = new FileSavePicker();
fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

var filedb = new[] { ".db" };
fileSavePicker.FileTypeChoices.Add("DB", filedb);
fileSavePicker.SuggestedFileName = "BACKUPDB" + System.DateTime.Now.Day + "-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Year;

//var pathDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "file.db");

try
{
    StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("file.db");
    StorageFile localfile = await fileSavePicker.PickSaveFileAsync();
    fileSavePicker.SuggestedSaveFile = file;

    if (file != null)
    {
        Debug.WriteLine("file Exists!!"); 
        var fileToSave = await fileSavePicker.PickSaveFileAsync();

        ....

but my saved file has size 0.

I found how to save text files but my file not is text.

Decade Moon
  • 32,968
  • 8
  • 81
  • 101
Rodrigo
  • 3
  • 1

1 Answers1

3

You can use CopyAndReplaceAsync method to copy your local file to the chosen file.

var fileSavePicker = new Windows.Storage.Pickers.FileSavePicker();
fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

var filedb = new[] { ".db" };
fileSavePicker.FileTypeChoices.Add("DB", filedb);
fileSavePicker.SuggestedFileName = "BACKUPDB" + System.DateTime.Now.Day + "-" + System.DateTime.Now.Month + "-" + System.DateTime.Now.Year;

//var pathDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "file.db");

try
{
    StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("file.db");
    StorageFile localfile = await fileSavePicker.PickSaveFileAsync();

    if (file != null)
    {
        Debug.WriteLine("file Exists!!");
        await file.CopyAndReplaceAsync(localfile);
    }
}
catch(Exception ex)
{
    Debug.WriteLine(ex);
}
tao
  • 760
  • 1
  • 7
  • 16
  • Perfect, thanks, i did too using this: https://stackoverflow.com/questions/15305758/export-sqlite-database-from-windows-store-app/15309471#15309471 but the your is better because more faster. Now, i will procedure reverse with FileOpenPicker() – Rodrigo Nov 02 '16 at 03:57