简体   繁体   English

无法绕过我的相对路径

[英]Can't get my head round relative path

This is probably something mind-numbingly obvious, but I'm new to c# so be gentle... 这可能是令人难以置信的显而易见的事情,但是我是C#的新手,所以要格外温柔...

I have an application which (in theory) parses a text file into an array. 我有一个应用程序(理论上)将文本文件解析为数组。 Despite the text file being a peer of the aspx file I can't get the relative path right. 尽管文本文件是aspx文件的同级文件,但我无法正确获得相对路径。 Don't know if it makes any difference (I'd assume not) but I'm using code-behind. 不知道它是否有任何区别(我想没有),但是我正在使用代码隐藏功能。

My folder structure looks like this: 我的文件夹结构如下所示:

  • default.aspx Default.aspx的
  • default.aspx.cs default.aspx.cs
  • default.aspx.designer.cs default.aspx.designer.cs
  • album.cs album.cs
  • albums.txt albums.txt
  • web.config web.config中

And this is the code I'm using: 这是我正在使用的代码:

 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];
       }
   }

However, when I build it I get an error saying albums.txt can not be found, and it fails. 但是,当我构建它时,出现错误,提示无法找到albums.txt,并且失败。

Any pointers would be greatly appreciated. 任何指针将不胜感激。

Ben

Server.MapPath specifies the relative or virtual path to map to a physical directory. 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)

An example: 一个例子:

Let's say you pointed a web site application ( http://www.example.com/ ) to 假设您将网站应用程序( http://www.example.com/ )指向了

C:\\Inetpub\\wwwroot C:\\的Inetpub \\ wwwroot文件

and installed your shop application (sub web as virtual directory in IIS, marked as application) in 并在以下位置安装了商店应用程序(IIS中的子网站为虚拟目录,标记为应用程序)

D:\\WebApps\\shop d:\\ WebApps的\\店

If, for example, you call Server.MapPath in following request: 例如,如果您在以下请求中调用Server.MapPath:

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

then, 然后,

* 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

If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path. 如果Path以正斜杠(/)或反斜杠()开头,则MapPath方法将返回路径,就好像Path是完整的虚拟路径一样。

If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed. 如果Path不以斜杠开头,则MapPath方法返回相对于正在处理的请求目录的路径。

Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences. 注意:在C#中,@是原义文字字符串运算符,表示该字符串应“按原样”使用,而不应针对转义序列进行处理。

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\\"), Server.MapPath("/"). Server.MapPath(“。”),Server.MapPath(“〜”),Server.MapPath(@“ \\”),Server.MapPath(“ /”)。 What is the difference? 有什么区别?

Instead of just the filename, use Server.MapPath(filename) to get the full path to the file. 使用Server.MapPath(filename)来获取Server.MapPath(filename)的完整路径,而不仅仅是文件名。

If the file is located in a different directory, you could use Server.MapPath("~/path/to/the/file.txt") , where ~ corresponds to the root folder of your web application. 如果文件位于其他目录中,则可以使用Server.MapPath("~/path/to/the/file.txt") ,其中〜对应于Web应用程序的根文件夹。

ReadAllLines takes an absolute path - what you've provided is a relative path. ReadAllLines采用绝对路径-您提供的是相对路径。 Server.MapPath is used to translate relative paths to absolute ones. Server.MapPath用于将相对路径转换为绝对路径。 Server.MapPath("~/Albums.txt") would give the right value irrespective of where the code resides. Server.MapPath(“〜/ Albums.txt”)将提供正确的值,而不管代码位于何处。 Also, by putting the file under ~\\App_Data\\ you can prevent direct downloads of the file itself as well as insulating the application against repeated updates to that file while the application is running (updates to App_Data contents don't generate File Change Notifications ). 另外,通过将文件放在〜\\ App_Data \\下,您可以防止文件本身的直接下载,并使应用程序在应用程序运行时避免对该文件的重复更新(对App_Data内容的更新不会生成文件更改通知 ) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM