简体   繁体   English

如何使用FileSavePicker UWP从我的应用程序复制文件

[英]how to copy file from my app Using FileSavePicker UWP

I want copy file in my app to a folder with FileSavePicker. 我想将我的应用程序中的文件复制到具有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. 但我保存的文件的大小为0。

I found how to save text files but my file not is text. 我发现了如何保存文本文件,但我的文件不是文本。

You can use CopyAndReplaceAsync method to copy your local file to the chosen file. 您可以使用CopyAndReplaceAsync方法将本地文件复制到所选文件。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM