Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\TextFiles\ActiveUsers.txt'

You need to use HttpServerUtility.MapPath which will turn the ~/ portion of the path in to the real location it resildes on your hard drive.

So that would change your code to (assuming you are in one of the IIS classes that expose a Server property to it's methods)

var path = Server.MapPath(@"~/TextFiles/ActiveUsers.txt");

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
{
    file.WriteLine(model.UserName.ToString());
}

I ran into a similar issue and ended up using

string sFileName = HttpContext.Current.Server.MapPath(@"~/dirname/readme.txt");

Tags:

C#