【问题标题】:Can't get my head round relative path无法绕开相对路径
【发布时间】:2010-01-05 18:46:41
【问题描述】:

这可能是显而易见的事情,但我是 c# 新手,所以要温柔...

我有一个应用程序(理论上)将文本文件解析为数组。尽管文本文件是 aspx 文件的对等文件,但我无法正确获取相对路径。不知道它是否有任何区别(我假设没有),但我正在使用代码隐藏。

我的文件夹结构如下:

  • default.aspx
  • default.aspx.cs
  • default.aspx.designer.cs
  • 专辑.cs
  • 专辑.txt
  • web.config

这是我正在使用的代码:

 protected void Page_Load(object sender, EventArgs e)
    {

         string[] allLines = File.ReadAllLines(@"Albums.txt");
         Album[] Albums = new Album[allLines.Length];
         for (int i = 0; i < allLines.Length; i++)
         {
           string[] lineSplit = allLines[i].Split(',');
           Albums[i] = new Album();
           Albums[i].ID = Convert.ToInt32(lineSplit[0]);
           Albums[i].title = lineSplit[1];
           Albums[i].keyName = lineSplit[2];
       }
   }

但是,当我构建它时,我收到一条错误消息,提示找不到 albums.txt,它失败了。

任何指针将不胜感激。

【问题讨论】:

  • 你试过用Server.MapPath("Albums.txt")
  • 这个网站是站点吗?还是网络项目?即有没有为此的csproj?我想知道您是否只是没有告诉它 txt 文件应该包含在构建输出中。 (复制到输出目录)
  • 你是个传奇!工作了一个款待。 Stack Overflow 也是新手 - 我是否需要做点什么来分配积分/确认此修复是否正确?
  • Server.MapPath 对其进行了排序 - 感谢 Omu(也感谢 Marc 的及时响应。)
  • @Ben,你在评论谁,Omu 还是 Marc?在 SO 上,cmets 和答案会根据您查看帖子的时间和方式进行洗牌。因此,为清楚起见,请使用“@username”处理其他 cmets/answers。至于分数,正确的结果必须在答案中,而不是在评论中,才能获得答案积分(您可以通过单击将鼠标悬停在评论上时出现的向上箭头来对评论进行投票)。找到有效的答案(Server.MapPath?),单击左侧分数下方的复选标记。这“接受”答案为最佳答案(根据您的判断),并奖励其作者。

标签: c# asp.net


【解决方案1】:

Server.MapPath 指定映射到物理目录的相对或虚拟路径。

* Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
* Server.MapPath("..") returns the parent directory
* Server.MapPath("~") returns the physical path to the root of the application
* Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

一个例子:

假设您将网站应用程序 (http://www.example.com/) 指向

C:\Inetpub\wwwroot

并在

中安装了您的商店应用程序(子网站作为 IIS 中的虚拟目录,标记为应用程序)

D:\WebApps\shop

例如,如果您在以下请求中调用 Server.MapPath:

http://www.example.com/shop/product/GetProduct.aspx?id=2342

那么,

* Server.MapPath(".") returns D:\WebApps\shop\products
* Server.MapPath("..") returns D:\WebApps\shop
* Server.MapPath("~") returns D:\WebApps\shop
* Server.MapPath("/") returns C:\Inetpub\wwwroot
* Server.MapPath("/shop") returns D:\WebApps\shop

如果 Path 以正斜杠 (/) 或反斜杠 () 开头,则 MapPath 方法返回一个路径,就好像 Path 是一个完整的虚拟路径。

如果 Path 不以斜杠开头,则 MapPath 方法返回相对于正在处理的请求的目录的路径。

注意:在 C# 中,@ 是逐字字面字符串运算符,表示该字符串应“按原样”使用,而不是针对转义序列进行处理。

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

【讨论】:

【解决方案2】:

不只是文件名,而是使用Server.MapPath(filename) 来获取文件的完整路径。

如果文件位于不同的目录中,您可以使用Server.MapPath("~/path/to/the/file.txt"),其中 ~ 对应于您的 Web 应用程序的根文件夹。

【讨论】:

    【解决方案3】:

    ReadAllLines 采用绝对路径 - 您提供的是相对路径。 Server.MapPath 用于将相对路径转换为绝对路径。 Server.MapPath("~/Albums.txt") 将给出正确的值,而与代码所在的位置无关。此外,通过将文件放在 ~\App_Data\ 下,您可以防止直接下载文件本身,并防止应用程序在应用程序运行时重复更新该文件(对 App_Data 内容的更新不会'不生成文件更改通知)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多