Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find path in the web application

I am trying to write to a text file usingASP.NET 4.5 with c# using the following code:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"./Experiment/main.txt", true))
{
file.WriteLine(DateTime.UtcNow.ToString() + " test");
}

And I am getting this exception:

"Could not find a part of the path 'C:\Windows\SysWOW64\inetsrv\Experiment\main.txt'."

The folder Experiment is the folder of my web application.

like image 695
user2992015 Avatar asked Feb 14 '23 16:02

user2992015


1 Answers

You need to give physical path instead of relative path, use Server.MapPath("~") to get the root path of your site and then append the path of file to it. You can learn more about Server.MapPath in this post.

using (System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath(@"~/Experiment/main.txt"), true))
{
     file.WriteLine(DateTime.UtcNow.ToString() + " test");
}
like image 195
Adil Avatar answered Feb 26 '23 18:02

Adil