6

Ok so I believe I am doing something very easy here. I have written an ASP.NET web page and it is just trying to write to the local directory.

I am using the following code:

System.IO.File.WriteAllLines("log.txt", messages);

I am throwing the following exception.

Access to the path 'c:\windows\system32\inetsrv\log.txt' is denied.

My ASP.NET application sits in the following directory.

c:\inetpub\wwwroot\sites\mysite\

So I am confused as to why it is trying to write to c:\windows\system32\inetsrv\ directory when I am not supplying that directory itself.

I have tried changing the code to the following but it gives me the same error message with a new directory.

System.IO.File.WriteAllLines("c:\\inetpub\\wwwroot\\sites\mysite\log.txt", messages);

Edit 1

It was hard to accept an answer on this because everyone really helped me out a ton. I accepted tom_yes_tom's answer because he was the first to post his response which was the first half of my problem. The other half of my problem was related to hbrock's solution that Fabio pointed out.

1
  • What identity is your ASP.NET application's app pool running as?
    – Ta01
    Commented Sep 12, 2012 at 17:38

3 Answers 3

6

Create the folder "C:\inetpub\wwwroot\sites\mysite\App_Data" and save your data there instead.

System.IO.File.WriteAllLines(Server.MapPath("~/App_Data/log.txt"))

If you absolutely must save it to the mysite directory, be aware of security ramifications of putting a log file there and set directory permissions appropriately. The user that IIS is running under needs to be able to read and write that directory.

Full qualifying path: System.Web.HttpContext.Current.Server

1
  • I used this approach and still got the same error. Access to the path 'C:\inetpub\wwwroot\sites\mysite\production\App_Data\log.txt' is denied Commented Sep 12, 2012 at 18:48
3

You're receiving "Access to the path 'c:\windows\system32\inetsrv\log.txt' is denied." when you execute System.IO.File.WriteAllLines("log.txt", messages) because c:\windows\system32\inetsrv is the directory where the IIS executable is located (w3wp.exe).

You have to use Server.MapPath so it gives you the physical path of your virtual directory.

Look which user is running your virtual directory's application pool, and give him write permissions on the folder of your virtual directory.

3
  • Ok so I looked at the application pool and it says its Identity->NetworkService. NetworkService is not on the permissions list for the actual site directory. Commented Sep 12, 2012 at 20:50
  • Follow @hbrock's intructions to give write permission on your folder for NETWORK SERVICE
    – Fabio
    Commented Sep 12, 2012 at 23:50
  • Ok I am going to try this again. I had attempted to do NetworkService without a space so that might have been my problem. Commented Sep 14, 2012 at 1:36
1

This should help:

To grant read, write, and modify permissions to a specific file

In Windows Explorer, locate and select the required file.

Right-click the file, and then click Properties.

In the Properties dialog box, click the Security tab.

On the Security tab, examine the list of users. If the Network Service account is not listed, add it.

In the Properties dialog box, click the Network Service user name, and in the Permissions for NETWORK SERVICE section, select the Read, Write, and Modify permissions.

Click Apply, and then click OK

information from: http://msdn.microsoft.com/en-us/library/ff647402.aspx#paght000015_fileaccess

1
  • I also had this security issue. My first attempt was to add NetworkService without a space and it didn't recognize a security object so I gave up on that approach, but as @Fabio Gouw pointed out it needed a space. Commented Sep 14, 2012 at 1:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.