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

后端 未结 4 1932
滥情空心
滥情空心 2021-02-19 10:13

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 10:48

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

提交回复
热议问题