Coder Perfect

The path is closed off to you.

Problem

In.NET C#, I’m attempting to save an image to a folder, but I’m getting the following exception:

Access to the path 'C:\inetpub\wwwroot\mysite\images\savehere' is denied.The error occured at mscorlib because    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)
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)

I provided network service and iis iusrs full control over this folder (savehere), and I even granted full control to everyone, but I still receive this exception. I tried giving access using explorer and IIS manager, but it didn’t work.

Who do I need to give access to if I’m running it on Windows Server 2008 R2 and IIS 7.5?

Asked by Burjua

Solution #1

Pay close attention to the message. You’re attempting to save to a file that shares the directory’s name. That won’t work since you can’t overwrite a whole directory of files with a single new file. That would result in unrecoverable data loss, and “Access to the path is refused” is the file system’s response to that.

The exception message isn’t perfect, but it comes directly from the operating system, and it’s set in stone. Extra tests are frequently added to the framework in order to provide better messages, however this is a costly network test. Perf is also a feature.

Use a name like ‘C:inetpubwwwrootmysiteimagessaveheremumble.jpg’ to save the image. Take a look at Path. To correctly produce the path name, use Combine().

Answered by Hans Passant

Solution #2

You must determine the identity that the website is running under (by default, this is Application Pool Identity) and grant that identity the appropriate rights.

Answered by Oded

Solution #3

When I tried to create a file on the server, I ran into the same issue (actually a file that is a copy from a template).

The entire error message is as follows:

{ERROR} 08/07/2012 22:15:58 - System.UnauthorizedAccessException: Access to the path 'C:\inetpub\wwwroot\SAvE\Templates\Cover.pdf' is denied.

Within the IIS app folder, I created a new folder called Templates. In my case, I needed to give the IUSR user the Write (Gravar) permission on that folder. You may also need to grant the same Write permission to Network Service and ASP.NET v$.#.

Everything works as intended after doing this.

Answered by Leniel Maccaferri

Solution #4

I experienced the exact same issue.

The problem was that the file I was trying to open was readonly because it was cloned from a readonly template file.

Answered by Ruskin

Solution #5

My issue was that I needed to request Read-Only access:

FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read);

Answered by jesal

Post is based on https://stackoverflow.com/questions/4877741/access-to-the-path-is-denied