Steven Combs
Steven Combs

Reputation: 1939

How do I configure my ASP.NET directory to allow writing a log file?

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.

Upvotes: 7

Views: 16495

Answers (3)

earth_tom
earth_tom

Reputation: 831

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

Upvotes: 6

Fabio
Fabio

Reputation: 3120

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.

Upvotes: 3

user751651
user751651

Reputation:

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

Upvotes: 1

Related Questions