How to configure an ASP.NET directory to write a log file?

Ok, so I think I'm doing something very simple here. I wrote an ASP.NET webpage and it just tries to write to a local directory.

I am using the following code:

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

I throw the following exception.

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

The ASP.NET application is located in the following directory.

 c:\inetpub\wwwroot\sites\mysite\ 

So, I am confused by why he is trying to write the directory c: \ windows \ system32 \ inetsrv \ when I do not supply this directory myself.

I tried changing the code to the next, but it gives me the same error message with the new directory.

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

Change 1

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

+7
source share
3 answers

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

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

If you absolutely must save it in the mysite directory, keep in mind the consequences for protecting against placing the log file there and choosing the right directory. The user who runs IIS should be able to read and write this directory.

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

+7
source

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

You must use Server.MapPath so that it provides you with a physical path to your virtual directory.

Look at which user is launching the virtual directory application pool and give him write permissions in the folder of your virtual directory.

+3
source

This should help:

Grant read, write, and modify permissions to a specific file

In Windows Explorer, locate and select the desired file.

Right-click on the file and select Properties.

In the Properties dialog box, click the Security tab.

On the Security tab, review the list of users. If the network service account is not listed, add it.

In the Properties dialog box, select the network service username and in the Permissions for the NETWORK Service section, select 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
source

All Articles