DotNetZip System.UnauthorizedAccessException: Access to the path is denied

StackOverflow https://stackoverflow.com/questions/22318581

  •  12-06-2023
  •  | 
  •  

Pergunta

I know here is some questions about this error in DotNetZip, I've tried all solutions and failed. I'm developing code on my Win8.1 notebook and have no problems, problems begin after deploy to the remote Win2008R2 server...

Here is the problem method:

public bool CreateZIP(string ListStep)
{
    Logger logger = LogManager.GetLogger("Task:CreateZIP" + this.TaskGuid);

    using (ZipFile zip = new ZipFile())
    {
        zip.AlternateEncoding = System.Text.Encoding.GetEncoding("cp866");
        zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always;

        ...
        zip.AddFile(...) loop
        ...

        zip.MaxOutputSegmentSize = Properties.Settings.Default.ZIPSizeLimit * 1024 * 1024;

        string zipFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"zip-tmp");
        string TaskZipFolder = Path.Combine(zipFolder, this.TaskGuid);

        try
        {
            if (!Directory.Exists(zipFolder)) Directory.CreateDirectory(zipFolder);
            if (!Directory.Exists(TaskZipFolder)) Directory.CreateDirectory(TaskZipFolder);

            zip.TempFileFolder = Path.GetTempPath();
            zip.Save(Path.Combine(TaskZipFolder, this.TaskGuid + @".zip"));
        }
        catch (Exception e)
        {
            logger.Fatal("Unable to save ZIP into ({0}): {1}", Path.Combine(TaskZipFolder, this.TaskGuid + @".zip"), e.ToString());
            throw;
        }
    }
    return true;
}

This code is running on remote server from domain user gfo-svc from C:\Courier\WD directory. Each object instance has it's GUID, for example e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55 So the zipFolder variable value is C:\Courier\WD\zip-tmp and TaskZipFolder value is C:\Courier\WD\zip-tmp\e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55

When this code tries to run it fails with this stack trace:

Unable to save ZIP into (C:\Courier\WD\zip-tmp\e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55\e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55.zip): System.UnauthorizedAccessException: Access to the path is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIOError()
   at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)
   at Ionic.Zip.ZipSegmentedStream.TruncateBackward(UInt32 diskNumber, Int64 offset)
   at Ionic.Zip.ZipEntry.Write(Stream s)
   at Ionic.Zip.ZipFile.Save()
   at Ionic.Zip.ZipFile.Save(String fileName)
   at Worker.Task.CreateZIP(String ListStep) in Worker.cs:line 844

Line 844 contains this code: zip.Save(Path.Combine(TaskZipFolder, this.TaskGuid + @".zip"));

But in the C:\Courier\WD\zip-tmp\e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55 folder I can see file e1664582-1bbc-4a9f-a9fe-e7ce4a0a8a55.z01, so the first part of archive was saved before failure.

gfo-svc user has ownership on C:\Courier\WD directory and my code could create zip-tmp directory if it is not exists, and GUID-named directory in it. So the problem is not in Windows Security permissions. UAC on this remote server is disabled.

What could I do wrong? What might be wrong with my environment? What could I do?

Foi útil?

Solução

Ok, here is the reason: This code runs twice on the same host: first time manually from cmd and second time from Windows Task Scheduler. By default, Task Scheduler starts tasks from C:\windows\system32 and process could not write into it. But my code tried to write into relative to working directory path, so in fact it was trying to write into ...\system32\tmp-dir.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top