Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用后台任务UWP启动备份文件夹_C#_Uwp_Windows Runtime_Task - Fatal编程技术网

C# 使用后台任务UWP启动备份文件夹

C# 使用后台任务UWP启动备份文件夹,c#,uwp,windows-runtime,task,C#,Uwp,Windows Runtime,Task,我必须使用后台任务启动文件夹备份,以便使用组件Windows运行时的implementTazione,但在组件Windows运行时中,不可能使用公共静态异步任务方法 MainPage.xaml.cs: public MainPage() { this.InitializeComponent(); } private async void BtnChooseFolder_Click(object sender, RoutedEventArgs e)

我必须使用后台任务启动文件夹备份,以便使用组件Windows运行时的implementTazione,但在组件Windows运行时中,不可能使用公共静态异步任务方法

MainPage.xaml.cs:

    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void BtnChooseFolder_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        folderPicker.FileTypeFilter.Add("*");
        StorageFolder SelectFolderToLoad = await folderPicker.PickSingleFolderAsync();
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("FolderToCopy", SelectFolderToLoad);
    }

    private async void BtnDestinationFolder_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        folderPicker.FileTypeFilter.Add("*");
        StorageFolder SelectFolderToLoad = await folderPicker.PickSingleFolderAsync();
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("DestinationFolder", SelectFolderToLoad);
    }

    private void BtnTask_Click(object sender, RoutedEventArgs e)
    {
        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {
            if (cur.Value.Name == "TaskLocalBackup")
            {
                cur.Value.Unregister(true);
            }
        }

        var requestTask = BackgroundExecutionManager.RequestAccessAsync();

        var builder = new BackgroundTaskBuilder();

        builder.Name = "TaskLocalBackup";
        builder.TaskEntryPoint = "ClassBackgraundTask.BackgroundTaskClass";
        //builder.SetTrigger(new SystemTrigger(SystemTriggerType.UserPresent, false));

        builder.SetTrigger(new TimeTrigger(15, false));

        BackgroundTaskRegistration task = builder.Register();
    }
BackgroundTaskClass.cs:(在组件Windows运行时中)

这就是错误:

方法“ClassBackgraundTask.BackgroundTaskClass.CopyFolderAsync(Windows.Storage.StorageFolder,Windows.Storage.StorageFolder)”的签名中的参数类型为“System.Threading.Tasks.Task”。尽管此类型不是有效的Windows运行时类型,但它实现的接口是有效的Windows运行时类型。考虑更改方法签名以使用下列类型之一:“Stask.IDISPOSTABLE”。ClassBackgraundTask

如何解决?


提前感谢。

您不应该自己编写bacakup程序。有很多备份程序。见鬼,在Windows上,我可以用Robocopy来做。事实上,我经常使用Robocopy进行备份。这些最简单的解决方案是将
CopyFolderAsync
private
,但您需要对
任务实例使用延迟。
    public sealed class BackgroundTaskClass : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            StartBackup();
        }
        private async static void StartBackup()
        {
            StorageFolder folderToSave = await 
                  StorageApplicationPermissions.FutureAccessList.GetFolderAsync("FolderToCopy");
            StorageFolder foldersSaved = await        
                 StorageApplicationPermissions.FutureAccessList.GetFolderAsync("DestinationFolder");
            await CopyFolderAsync(folderToSave, foldersSaved);
        }

        public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer)
        {
            StorageFolder destinationFolder = null;
            destinationFolder = await destinationContainer.CreateFolderAsync("Local" ?? source.Name, 
                                CreationCollisionOption.ReplaceExisting);

            foreach (var file in await source.GetFilesAsync())
            {
                await file.CopyAsync(destinationFolder, file.Name, 
                      NameCollisionOption.ReplaceExisting);
            }
            foreach (var folder in await source.GetFoldersAsync())
            {
                 await CopyFolderAsync(folder, destinationFolder);
            }
        }
    }