Error while trying to compress folders from a drive - Unauthorizedaccessexception

Asked

Viewed 80 times

-1

I need to compress all of my E folders: But when I try with this code, I get this access exception. I’m new to programming and I’m trying to learn how to do things right. I am using dotnetzip to compress the directories inside E: Some parts of the code are copied, I know that... but it was because all the solutions I tried myself didn’t work, so I just tried to implement things that seemed to work for other people

I’ve tried some other solutions to solve the problem answered here in the same Stack. How to add a manifesto to the project that uses requeireAdministrator to run the project, and insert an Accessrule access control rule to modify the security of E: One thing I notice when I do this is that my user has their permissions removed from sub-E files and directories: But without this access rule, the same exception still exists. I don’t know if I’m implementing it wrong...

Well, thank you for your attention to those who read so far! I’ll leave down here what I want to do with the program and the bug catch:

  try
    {

        ZipFile zip = new ZipFile();
        zip.AddDirectory(@"E:\");
        zip.Save(@"C:\Users\vitorbento\Desktop\backup.zip");

        Console.WriteLine("Compactação concluída");
        Console.WriteLine("Done.");
    }
    catch (UnauthorizedAccessException)
    {
        FileAttributes attr = (new FileInfo(DirectPath)).Attributes;
        Console.Write("UnAuthorizedAccessException: Unable to access file. ");
        if ((attr & FileAttributes.ReadOnly) > 0)
        Console.Write("The file is read-only.");
    }
  • Have you tried running the application as an administrator? (to debug, visual studio should open with administrator as well)

  • I tried to open Visual Studio as an administrator, as well as open the generated file as an administrator.

1 answer

0

Tried to use the static Zipfile class present in System.IO.Compression ?

You could use the Createfromdirectory method like this:

            ZipFile.CreateFromDirectory(@"E:\", @"C:\Users\vitorbento\Desktop\backup.zip");
            Console.WriteLine("Compactação concluída");
            Console.WriteLine("Done.");
  • How this would help with a permission error in the folder?

  • I tried to develop the same program with Zipfile, but I had the same problem with the permissions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.