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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With