I am trying to read/write to text file located at the root of my website. I am using asp.net development server (my local machine).
When I read the file it's throwing this exception
Access to the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\error.txt' is denied.
I am using following lines of code to read/write from text file
var text = File.ReadAllText(Server.MapPath("error.txt"));
File.WriteAllText(@"error.txt", text + " \n " + "newnewnew text");
What is the problem, why my server.MapPath retrieving path of my c drive? When it should retrieve absolute path of my site, that is
website1/error.txt
If you change following line
File.ReadAllText(Server.MapPath("error.txt"));
to
File.ReadAllText(Server.MapPath("~/error.txt"));
It will search file in root directory of application. If you have file stored in folder called "MyFiles" inside root directory then path will be something like below
File.ReadAllText(Server.MapPath("~/MyFiles/error.txt"));
Hope this helps.