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

后端 未结 4 2266
悲哀的现实
悲哀的现实 2021-02-19 10:07

I tried many ways to access a text file in my Visual Studio 2012 Solution from a folder named TextFiles

using (System.IO.StreamWriter file = new Sys         


        
4条回答
  •  情话喂你
    2021-02-19 11:06

    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());
    }
    

提交回复
热议问题