尝试解压缩到具有提升权限的文件夹时,我不断遇到异常。我试图避免必须从外部更改权限,因为它必须在大量计算机上。
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)
我想也许是因为该文件没有扩展名,但是解压缩到其他权限级别较低的文件夹确实允许您解压缩这样的文件。
我正在使用 Visual Studio Code 来开发和运行我的程序。我尝试以管理员身份运行,这解决了我遇到的一些其他权限相关问题,但无法解决此问题。我还尝试更改访问规则并更改所有者:
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);
您能否检查一下在解压缩过程中没有覆盖(针对同一文件夹的重复文件)文件?