StreamReader Server.MapPath - Physical path given, virtual path expected

怎甘沉沦 提交于 2019-12-13 05:15:06

问题


Im trying to get my .Net website to read the first line in a text file that shows a status of the computer it is located on. I get the good old "Server.MapPath - Physical path given, virtual path expected" if I remove the Server.MapPath, it fixes it the problem on the server, but then errors on the clients computer because it seems to be trying to access the file on the clients PC. Here is snippet:

        StreamReader StreamReader3 = new StreamReader(Server.MapPath(@"C:\\status\\status.txt"));
        TextBox2.Text = StreamReader3.ReadLine();
        StreamReader3.Close()

I know I probably could fix the issue by moving the file called to the same folder as the webpage, and have my server side program update the file there, but is there a way to do this otherwise? Thanks.


回答1:


MapPath translates paths starting from ~ to full local path. Does not look like you need it in your code.

If the file is supposed to be at fixed place all the time, open in directly by full path.

If it is relative to site root, then use something like Server.MapPath("~/status.txt")



来源:https://stackoverflow.com/questions/18277062/streamreader-server-mappath-physical-path-given-virtual-path-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!