1

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++)
         {
           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

Ben
  • 13
  • 3
  • Have you tried using Server.MapPath("Albums.txt") – Omu Jan 05 '10 at 18:48
  • Is this web **site**? or web **project**? i.e. is there a csproj for this? I'm wondering if you simply haven't told it that the txt file is meant to be included in the build output. (Copy to Output Directory) – Marc Gravell Jan 05 '10 at 18:52
  • You're a legend! Worked a treat. New to Stack Overflow too - do I need to do something to allocate points / confirm this fix is right? – Ben Jan 05 '10 at 18:54
  • Server.MapPath sorted it - thanks Omu (Cheers Marc for the prompt response, too.) – Ben Jan 05 '10 at 18:54
  • @Ben, who are you commenting to, Omu or Marc? On SO, the comments and answers get shuffled depending on when & how you view a post. So, address other comments/answers with "@username" for clarity. As for points, the correct result must be in an Answer, not a Comment, in order to award answer points (tho you can upvote a comment by clicking the up arrow that appears when you hover over the comment). Find the answer that worked (Server.MapPath?), click the check mark under the score number to the left. That 'accepts' the answer as the best one (by your judgement), & awards points to its author. – Val Jan 05 '10 at 19:00
  • @Ben: re your points question - look next to Omu's answer - there should be a "tick". Click on it. Welcome to the site ;-p – Marc Gravell Jan 05 '10 at 19:00
  • @Val - Cheers for the etiquette tip. I was commenting on Omu's response, but "tipping a nod" to Marc for his prompt response. Omu's answer was the one I used first. However, should I tick all the below answers that are right? Sorry to look like a numpty - just want to know as I'll definitely be using this again. – Ben Jan 05 '10 at 19:03

3 Answers3

3

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?

Community
  • 1
  • 1
Omu
  • 69,856
  • 92
  • 277
  • 407
  • Copied from https://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath#275791 ?? – Jacques Nov 03 '20 at 09:54
2

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.

M4N
  • 94,805
  • 45
  • 217
  • 260
1

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).

Nariman
  • 6,368
  • 1
  • 35
  • 50