【问题标题】:Access denied to a .tmp path拒绝访问 .tmp 路径
【发布时间】:2014-02-05 16:56:38
【问题描述】:

我正在尝试使用 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# asp.net .net winforms visual-studio-2010


    【解决方案1】:

    您正在尝试写入C:\Users 目录,但您没有这样做的权限。

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

    更多信息请参见http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx

    您可以按如下方式使用它:

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

    【讨论】:

    • 我不完全确定我应该如何以及在哪里使用它。你能写一个例子吗?
    • 它会抛出错误。它说访问路径被拒绝。
    • @orglce 哪个路径?临时路径,还是您使用 zip.Save(path + "\\SavedZip.zip"); 保存到的路径?
    • 我在 ASP.NET 应用程序中遇到了同样的错误。目标文件夹和临时文件夹对每个人都可以完全访问以尝试开始工作,所以不要认为是那样。此外,仅当我尝试使用 MaxOutputSegmentSize 创建多部分 zip 时,我的代码才会失败。不要尝试创建细分,它可以正常工作,因此拒绝访问来自其他地方
    【解决方案2】:

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

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

    以及保存时使用

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

    【讨论】:

    • 该文件夹可能不存在。事实上,这种驱动可能不存在。看我的回答。
    • 是的。可能这就是为什么我说尝试使用TempFileFolder 对象的ZipFile 属性设置已知文件夹。
    【解决方案3】:

    如果用户确实应该具有写入权限,请尝试在写入磁盘之前通过代码检查您是否具有写入权限。使用 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));
        }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      相关资源
      最近更新 更多