ZipFile.CreateFromDirectory löst eine System.UnauthorizedAccessException ausC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 ZipFile.CreateFromDirectory löst eine System.UnauthorizedAccessException aus

Post by Anonymous »

Ich erhalte ständig eine Ausnahme, wenn ich versuche, in einen Ordner mit erhöhten Berechtigungen zu entpacken. Ich versuche zu vermeiden, die Berechtigungen extern ändern zu müssen, da dies auf einer großen Anzahl von Computern erfolgen müsste.
System.Exception: Error transferring files
---> System.Exception: Error unzipping C:\TransferFiles\ZipFolder.zip to C:\ProgramData\Tecan
---> System.UnauthorizedAccessException: Access to the path C:\ProgramData\Tecan\SVNRoot\SystemSW_1.0\format 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)

Ich dachte, es könnte daran liegen, dass die Datei keine Erweiterung hatte, aber das Entpacken in andere Ordner mit niedrigerer Berechtigungsstufe ermöglicht es Ihnen, eine solche Datei zu entpacken.
Ich verwende Visual Studio Code, um mein Programm zu entwickeln und auszuführen. Ich habe versucht, als Administrator zu arbeiten, wodurch das Problem mit einigen anderen Berechtigungsproblemen gelöst wurde, die ich hatte, aber ich konnte das Problem nicht umgehen. Ich habe auch versucht, die Zugriffsregel und den Besitzer zu ändern:
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);

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post