繁体   English   中英

访问被拒绝到.tmp路径

[英]Access denied to a .tmp path

我正在尝试使用DotNetZip库压缩文件。 我正在读取文件的路径,并将zip文件保存到该文件。 但是程序崩溃并抛出。 这是我的代码:

using (ZipFile zip = new ZipFile())
{
   zip.AddDirectory(dir + "\\OUTPUT_FOLDERS");

   StreamReader sr = new StreamReader(dir + "\\Tools\\SettingsForPath");
   string path = sr.ReadToEnd();
   sr.Close();

   zip.Save(path + "\\SavedZip.zip");
   Directory.Delete(dir + "\\OUTPUT_FOLDERS", true);
}

这是我的错误:

System.UnauthorizedAccessException: Access to the path 'C:\Users\DotNetZip-nvan5kb5.tmp' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Ionic.Zip.SharedUtilities.CreateAndOpenUniqueTempFile(String dir, Stream& fs, String& filename)
at Ionic.Zip.ZipFile.get_WriteStream()
at Ionic.Zip.ZipFile.Save()
at Ionic.Zip.ZipFile.Save(String fileName)

您正在尝试写入C:\\Users目录,但您没有执行此操作的权限。

使用Path.GetTempPath()获取可写目录的名称。

有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/system.io.path.gettemppath.aspx

您将按以下方式使用它:

using (ZipFile zip = new ZipFile())
{
    zip.TempFileFolder = System.IO.Path.GetTempPath();

    // etc.

我认为问题在于您没有权限在哪个目录中创建临时文件。 尝试将临时文件夹设置为

zip.TempFileFolder = @"D:\tempfolder";

以及何时保存使用

zip.Save(@"D:\tempfolder\my.zip");

如果用户确实应该具有写访问权限,请尝试在光盘上刻录之前通过代码检查您是否具有写权限。.使用System.Security.Principal.WindowsIdentity.GetCurrent()。Name作为名称。.如果确实是临时使用如上所述的临时文件夹

string path = @"c:\temp";
string NtAccountName = @"MyDomain\MyUserOrGroup";

DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));

foreach (AuthorizationRule rule in rules)
{
 //If we find one that matches the identity we are looking for
 if (rule.IdentityReference.Value.Equals(NtAccountName,StringComparison.CurrentCultureIgnoreCase))
  {
    //Cast to a FileSystemAccessRule to check for access rights
    if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData)>0)
    {
        Console.WriteLine(string.Format("{0} has write access to {1}", NtAccountName, path));
    }
    else
    {
        Console.WriteLine(string.Format("{0} does not have write access to {1}", NtAccountName, path));
    }
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM