Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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 ZipFile.CreateFromDirectory_C#_File Io_Uwp - Fatal编程技术网

C# UWP ZipFile.CreateFromDirectory

C# UWP ZipFile.CreateFromDirectory,c#,file-io,uwp,C#,File Io,Uwp,我有以下代码 private static async Task CreateZipFile(string folderPath) { await Task.Run(() => { try { ZipFile.CreateFromDirectory(folderPath, ApplicationData.Current.LocalFolder.Path + "backup.zip"); }

我有以下代码

private static async Task CreateZipFile(string folderPath)
{
    await Task.Run(() =>
    {
        try
        {
            ZipFile.CreateFromDirectory(folderPath, ApplicationData.Current.LocalFolder.Path + "backup.zip");
        }
        catch (Exception e)
        {
            ;
        }
    });
}
在我的UWP应用程序中。但是,我在LocalFolder上遇到拒绝访问错误。 我试图压缩的目录位于我的LocalState文件夹中,因此

folderPath = C:\Users\username\AppData\Local\Packages\MyApp_3y0bchp7kwvet\LocalState\BACKUP

有什么办法解决吗?其他代码在访问这些文件夹时没有问题。

我认为这里的问题是您在方法中使用了错误的路径作为destinationArchiveFileName

对于有效路径,它应该是
ApplicationData.Current.LocalFolder.path+“\\backup.zip”

一旦您像下面这样更改了代码,它应该能够工作

ZipFile.CreateFromDirectory(folderPath, ApplicationData.Current.LocalFolder.Path + "\\backup.zip");

如果您是从其他软件包运行此代码,那么它现在将允许从其他应用程序的本地存储读取内容。ApplicationData.Current.LocalFolder仅指向LocalState文件夹。您完全正确。非常感谢您的回答。这不是严格意义上的UWP答案,因为ZipFile类是.NET库的一部分,而不是UWP库。它只对那些也在使用C#的人有用,但对使用另一种语言的人不起作用