Search code examples
asp.net-mvciis-7virtual-directory

Access to the path is denied in ASP.NET MVC


I was trying to Create a Directory using this code:

Directory.CreateDirectory("localhost/attachments/7075");

But it returned 'Access to the path 'localhost/attachments/7075' is denied.'

Please take note that the attachments is from a Network Shared Folder. I added it using the Add Virtual Directory on IIS.

What I did is:

  • Tried to create a folder manually in the Shared Folder and I can.
  • I tried to check the securities on folder the IIS_IUsers and it is in Full Control

Solution

  • You need to use Server.MapPath() to use the physical file path that corresponds to the virtual path.

    Directory.CreateDirectory(Path.Combine(Server.MapPath("~/attachments"), "7075"));