开发者

Can't get my head round relative path

This is probably something mind-numbingly obvious, but I'm new to c# so be gentle...

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. 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.cs
  • default.aspx.designer.cs
  • album.cs
  • albums.txt
  • 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++)
         {
   开发者_如何学C        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.

Any pointers would be greatly appreciated.

Ben


Server.MapPath specifies the relative or virtual path to map to a physical directory.

* 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

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

If, for example, you call Server.MapPath in following request:

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.

If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.

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.

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.

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.


ReadAllLines takes an absolute path - what you've provided is a relative path. Server.MapPath is used to translate relative paths to absolute ones. Server.MapPath("~/Albums.txt") would give the right value irrespective of where the code resides. 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜