orglce
orglce

Reputation: 513

Access denied to a .tmp path

I am trying to zip a file with DotNetZip library. I am reading path from a file and saving zip to that file. But program crashes and throws. This is my code:

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);
}

and here is my error:

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)

Upvotes: 4

Views: 9317

Answers (3)

Kris Vandermotten
Kris Vandermotten

Reputation: 10201

You are trying to write to the C:\Users directory, and you don't have permission to do that.

Use Path.GetTempPath() to obtain the name of directory where you can write.

See http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx for more information.

You would use it as follows:

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

    // etc.

Upvotes: 4

Marc Wittmann
Marc Wittmann

Reputation: 2362

If the user should really have write access try to check if you have write priviliges via code before writing on disc.. Use System.Security.Principal.WindowsIdentity.GetCurrent().Name for your name.. If it really is just temporary use the temp folder as stated above

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));
    }
 }
}

Upvotes: 0

Sameer
Sameer

Reputation: 2171

I think problem is in which directory your temporary files created you don't have permission. Try to set temporary folder like

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

and when save use

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

Upvotes: 1

Related Questions