问题
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