skip to Main Content

I keep getting an exception when trying to unzip to a folder with elevated permissions. I am trying to avoid having to externally change the permissions since it would have to be on a large number of computers.

System.Exception: Error transferring files
       ---> System.Exception: Error unzipping C:TransferFilesZipFolder.zip to C:ProgramDataTecan
       ---> System.UnauthorizedAccessException: Access to the path C:ProgramDataTecanSVNRootSystemSW_1.0format is denied.
         at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
         at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable1 unixCreateMode)
         at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable1 unixCreateMode)
         at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
         at System.IO.FileStream..ctor(String path, FileStreamOptions options)
         at System.IO.Compression.ZipFileExtensions.ExtractToFile(ZipArchiveEntry source, String destinationFileName, Boolean overwrite)
         at System.IO.Compression.ZipFileExtensions.ExtractRelativeToDirectory(ZipArchiveEntry source, String destinationDirectoryName, Boolean overwrite)
         at System.IO.Compression.ZipFileExtensions.ExtractToDirectory(ZipArchive source, String destinationDirectoryName, Boolean overwriteFiles)
         at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding, Boolean overwriteFiles)

I thought maybe it was because the file did not have an extension, but unzipping to other folders with lower permission level does allow you to unzip a file like that.

I am using Visual Studio Code to develop and run my program. I have tried running as an administrator, which solved the issue for some other permission related issues I was having but was not able to get around this. I also attempted to change the accessrule and change the owner:

DirectoryInfo directoryInfo = new DirectoryInfo(destination);
 DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
 FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(
 new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),
 FileSystemRights.FullControl,
 InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
 PropagationFlags.InheritOnly,
 AccessControlType.Allow);
 directorySecurity.AddAccessRule(fileSystemAccessRule);
 directoryInfo.SetAccessControl(directorySecurity);
DirectoryInfo directoryInfo = new DirectoryInfo(destination);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
var owner = directorySecurity.GetOwner(typeof(NTAccount));
var newOwner = new NTAccount(WindowsIdentity.GetCurrent().Name);
directorySecurity.SetOwner(newOwner);
directoryInfo.SetAccessControl(directorySecurity);

2

Answers


  1. Can you please check you are not overwriting (a duplicate file targeting to the same folder) a file during the unzipping process?

    Login or Signup to reply.
  2. Read here

    Add an app.manifest, the client will have to run your app(and run as admin). You should have access to the folder with elevated permissions.

    I use a similar approach to installing ffmpeg within system32 folder

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search