0

I'm trying to use SharpCompress to read .rar files from my UWP application. It works fine on network shares from which I can read the archive no problem, but I get System.UnauthorizedAccessException on files anywhere on the local system including for instance USB drives. I have access to the files by other methods e.g. StorageFile. It makes no difference whether BroadFileSystemAccess is on or off. I've tried in both C# and Vb.net Here's the code of my test app in C#. The exception occurs at ArchiveFactory.Open.

I can also read Zip files no problem using the .net Compression methods but they can't do rar files, hence needing SharpCompress.

using System;
using System.IO;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using SharpCompress;
using Windows.Storage.Pickers;
using Windows.Storage;
using SharpCompress.Archives;

namespace TestRAR
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            OpenRAR.Click += OpenRAR_Clicked;
        }

        public async void OpenRAR_Clicked(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".rar");
            picker.FileTypeFilter.Add(".cbr");
            picker.FileTypeFilter.Add(".cbz");
            picker.FileTypeFilter.Add(".zip");
            StorageFile pickfile = await picker.PickSingleFileAsync();
            if (pickfile == null) { return; }

            string pth = pickfile.Path;
            FileInfo pickInfo = new FileInfo(pth);

            try
            {
                ListRARs.Items.Clear();
                using (var Arch = ArchiveFactory.Open(pickInfo))
                {
                   foreach (IArchiveEntry a in Arch.Entries) 
                    {
                        string thisKey = a.Key;
                        ListRARs.Items.Add(thisKey);
                    }
                }
            }
            catch{ }

        }

    }
}

This is the first time I've used SharpCompress and I'm completely stumped. Any ideas anyone?

Ian Bland
  • 101
  • 1
  • Explained in detail [here](https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions). – Hans Passant Sep 06 '20 at 13:51
  • @HansPassant that doesn't answer my question. Please read it again. – Ian Bland Sep 06 '20 at 13:57
  • @IanBland maybe you should specify your question instead of asking such a broad one? – darclander Sep 06 '20 at 14:07
  • I don't understand what's too broad about asking why SharpCompress is throwing an unauthorized access exception when no other form of file access does, and only on local files. What would you like me to specify? – Ian Bland Sep 06 '20 at 14:10
  • 1
    @IanBland If you can open a RAR file through StorageFile then perhaps you could use that to open it as a stream ([Getting a Stream from a resource file / content](https://stackoverflow.com/q/13856352/1115360)) and then it looks like the SharpCompress [`ReaderFactory.Open(stream)`](https://github.com/adamhathcock/sharpcompress/blob/master/USAGE.md) method would be used to process that stream. – Andrew Morton Sep 06 '20 at 16:33
  • @AndrewMorton thanks I'll try that if nothing else works. – Ian Bland Sep 06 '20 at 23:37

0 Answers0