none
System.UnauthorizedAccessException: Access to the path denied

    Question

  • Hi,

    I am trying to create function that create xml and then compress it into zip file. Here is the code

      private FileResult writeXml(ImportBase import, string name)
      {
           System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(ImportBase));
           name = name.Replace("\n", " ");
           var destinationFilePath = Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + @"\" + name + @".xml";
           var destinationZipPath = Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
           System.IO.StreamWriter file = new System.IO.StreamWriter(destinationFilePath);
           writer.Serialize(file, import);
           file.Close();
    
           ZipFile.CreateFromDirectory(destinationFilePath, destinationZipPath);
           return File(destinationZipPath, "application/zip");
     }

    The problem by this code that by this belwo line, I got error  System.UnauthorizedAccessException: Access to the path denied

    ZipFile.CreateFromDirectory(destinationFilePath, destinationZipPath)

    Can anyone help me what should I do to get away from this error?

    Many Thanks



    • Edited by wapt49 Thursday, June 18, 2015 3:25 PM
    Thursday, June 18, 2015 3:23 PM

Answers

  • This is an MVC app.  Questions related to MVC should be posted in the ASP.NET forums (http://forums.asp.net ).  The reason why this matters is because you are trying to write to the documents directory of the current user but that user would be the identify of the app pool running your site and web apps don't have write permission outside their root directory by default.  Hence you're running into a problem with how web apps run.

    The correct solution is to create a temp directory in your web app which your app pool has write permissions to and then write to that folder.  Alternatively you may be able to use HttpRuntime.CodegenDir which is where ASP.NET drops the code gen files. 

    Please post further questions related to MVC in the ASP.NET forums (http://forums.asp.net ).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by wapt49 Friday, June 19, 2015 8:00 AM
    Thursday, June 18, 2015 4:01 PM
    Moderator

All replies

  • This is an MVC app.  Questions related to MVC should be posted in the ASP.NET forums (http://forums.asp.net ).  The reason why this matters is because you are trying to write to the documents directory of the current user but that user would be the identify of the app pool running your site and web apps don't have write permission outside their root directory by default.  Hence you're running into a problem with how web apps run.

    The correct solution is to create a temp directory in your web app which your app pool has write permissions to and then write to that folder.  Alternatively you may be able to use HttpRuntime.CodegenDir which is where ASP.NET drops the code gen files. 

    Please post further questions related to MVC in the ASP.NET forums (http://forums.asp.net ).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by wapt49 Friday, June 19, 2015 8:00 AM
    Thursday, June 18, 2015 4:01 PM
    Moderator
  • Hi wapt,

    From MSDN document, I got as below:

    UnauthorizedAccessException

    destinationArchiveFileName specifies a directory.

    -or-

    The caller does not have the required permission to access the directory specified in sourceDirectoryName or the file specified in destinationArchiveFileName.

    Per my understanding, I guess that your app pool user or anonymous user doesn't have proper permissions to the file itself.   If this is on IIS6, the Asp.Net user isn't used anymore.  Make sure to check your anonymous user also has permissions. 

    Please refer to the following thread for detailed information

    How to assign permissions to ApplicationPoolIdentity account

    If above solution still cannot resolve this issue, please consider posting in Asp.Net forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, June 19, 2015 6:28 AM
    Moderator
  • The bottom line here is the Application Pool the MVC application is running under in IIS is presenting credentilas that don't have the permissions to write a file to the folder., and the O/S is has denied access.

    https://www.bluevalleytech.com/techtalk/blog/assigning-ntfs-folder-permission-to-iis7-application-pools.aspx

    Friday, June 19, 2015 2:08 PM