StreamReader Server.MapPath - 给定物理路径,需要虚拟路径 [英] StreamReader Server.MapPath - Physical path given, virtual path expected

查看:47
本文介绍了StreamReader Server.MapPath - 给定物理路径,需要虚拟路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的 .Net 网站读取文本文件中的第一行,该文件显示了它所在的计算机的状态.如果我删除 Server.MapPath,我会得到很好的旧Server.MapPath - 给出的物理路径,预期的虚拟路径",它修复了服务器上的问题,但随后在客户端计算机上出现错误,因为它似乎正在尝试访问客户端 PC 上的文件.这是片段:

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.

推荐答案

MapPath 将从 ~ 开始的路径转换为完整的本地路径.看起来您的代码中不需要它.

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.

如果是相对于站点根目录,则使用类似 Server.MapPath("~/status.txt") 的内容

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

这篇关于StreamReader Server.MapPath - 给定物理路径,需要虚拟路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆