简体   繁体   English

使用System.IO的UWP broadFileSystemAccess读取和提取rar文件

[英]UWP broadFileSystemAccess using System.IO to read and extract rar files

I am targeting build 17134 and trying to use system.IO to get the files in a directory. 我的目标是内部版本17134,并尝试使用system.IO来获取目录中的文件。 I'm also using sharpcompress to extract .rar files. 我还使用Sharpcompress提取.rar文件。 From my understanding/what I've read online the new restricted capability "broadFileSystemAccess" should allow my app full access to any directory. 根据我的理解/在线阅读的内容,新的受限功能“ broadFileSystemAccess”应允许我的应用完全访问任何目录。 My code is below and ATM I pass in a hard coded directory on my E drive. 我的代码在下面,我通过ATM进入E驱动器上的硬编码目录。

    public void DirSearch(string sDir)
    {
        var ext = new List<string> { "*.rar", "*.zip" };

        foreach (String fileExtension in ext)
        {
            foreach (String file in Directory.EnumerateFiles(sDir, fileExtension, SearchOption.AllDirectories))
            {
                var archive = ArchiveFactory.Open(file);
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        Console.WriteLine(entry.Key);
                        entry.WriteToDirectory(@"E:\temp\", new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
                    }
                }

                Debug.WriteLine(file);
            }
        }
    }

This does not seem to work for me as I'm always getting: 这似乎对我不起作用,因为我总是得到:

System.UnauthorizedAccessException: 'Access to the path 'E:\\rars' is denied.'

I've also tried using the folder picker + storageFile which does list the rar files but then the following line throws an UnauthorizedAccessException error: 我也尝试过使用文件夹选择器+ storageFile,它确实列出了rar文件,但是以下行引发了UnauthorizedAccessException错误:

var archive = ArchiveFactory.Open(file);

How can I read and write to my E drive using System.IO and Sharpcompress using the broadFileSystemAccess capability? 如何使用System.IO和Sharpcompress使用broadFileSystemAccess功能读写E驱动器? What am I missing? 我想念什么?

As the introduction in the document broadFileSystemAccess capability and Accessing additional locations , 作为文件broadFileSystemAccess功能访问其他位置的介绍

This capability works for APIs in the Windows.Storage namespace. 此功能适用于Windows.Storage命名空间中的API。

So you still can not get the file directly by the file's path like E:\\ using the sharpcompress Apis. 因此,您仍然无法使用Sharpcompress Apis通过文件路径(如E:\\直接获取文件。 But you can use the StorageFolder.GetFolderFromPathAsync Api to get the folder then use the ArchiveFactory.Open(Stream stream, ReaderOptions readerOptions = null) method to exact the file. 但是您可以使用StorageFolder.GetFolderFromPathAsync Api来获取文件夹,然后使用ArchiveFactory.Open(Stream stream, ReaderOptions readerOptions = null)方法来精确化文件。

Here are some example code, you can put it as a reference. 这是一些示例代码,您可以将其作为参考。

public async void DirSearch(string sDir)
{
    var ext = new List<string> { ".rar", ".zip" };
    var folder = await StorageFolder.GetFolderFromPathAsync(sDir);
    var exactFile = await folder.CreateFileAsync("newFile", CreationCollisionOption.GenerateUniqueName);
    using (var writeStream = await exactFile.OpenStreamForWriteAsync())
    {
        foreach (var file in await folder.GetFilesAsync())
        {
            if (ext.Contains(file.FileType))
            {
                var stream = await file.OpenReadAsync();
                var archive = ArchiveFactory.Open(stream.AsStream());
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        Console.WriteLine(entry.Key);
                        entry.WriteTo(writeStream);
                    }
                }
                Debug.WriteLine(file);
            }
        }
    }
}

--- Update --- - 更新 -

Since UWP app have file access permission, you can not use none Windows.Storage namespace api to access the file path like E:\\foldre\\ directly, but your app have the full permission to the local folder, you can use IArchiveEntry.WriteToDirectory(string destinationDirectory) method to exact the files to the local folder, then you can copy the files to the folder location you want, try the following code, 由于UWP应用具有文件访问权限,因此您不能使用Windows.Storage名称空间api直接访问文件路径,例如E:\\foldre\\ ,但是您的应用具有对本地文件夹的完全权限,因此可以使用IArchiveEntry.WriteToDirectory(string destinationDirectory)方法将文件精确到本地文件夹,然后可以将文件复制到所需的文件夹位置,请尝试以下代码,

public async void DirSearch(string sDir)
{
    var ext = new List<string> { ".rar", ".zip" };
    var folder = await StorageFolder.GetFolderFromPathAsync(sDir);
    var ExactLocalFolder =await ApplicationData.Current.LocalFolder.CreateFolderAsync("ExactLocalFolder");
    foreach (var file in await folder.GetFilesAsync())
    {
        if (ext.Contains(file.FileType))
        {
            var stream = await file.OpenReadAsync();
            var archive = ArchiveFactory.Open(stream.AsStream());
            foreach (var entry in archive.Entries)
            {
                if (!entry.IsDirectory)
                {
                    Console.WriteLine(entry.Key);
                    //extract the files to local folder
                    entry.WriteToDirectory(ExactLocalFolder.Path);
                }
            }
        }
    }
    //copy the files to the folder that you want to save the extract file into
    foreach (var file in await ExactLocalFolder.GetFilesAsync())
    {
        await file.CopyAsync(folder);
    }
    //delete the folder in the local folder
    await ExactLocalFolder.DeleteAsync();
}

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

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