0

I'm building a webapp using ASP.NET.

On my physical hard drive:

The path for my text file is: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Data\TextFiles\someFile.txt

The .cs file is located in: D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\someCSFile.cs

In my code, I have the followings:

string fileName= Server.MapPath("TextFile/someFile.txt");

The code throws an exception saying that Could not find a part of the path 'D:\Users\(MyName)\Documents\Visual Studio 2013\Projects\(ProjectName)\(ProjectName)\Account\TextFile\someCSFile.cs

How am I going to use Server.MapPath to make it "go up one level", then find the "Data" folder > "TextFiles" > finally the "someFile.txt" WITHOUT hardcoding the entire file path?

C.J.
  • 2,867
  • 6
  • 27
  • 44

2 Answers2

3

This should do the trick

string fileName= Server.MapPath(@"..\Data\TextFile\someFile.txt");

take a look at this: StackOverflow Post about Server.MapPath

Community
  • 1
  • 1
Brent
  • 548
  • 3
  • 12
2

You can use .. to go up one level:

string fileName= Server.MapPath("../Data/TextFile/someFile.txt");

You can also start from the application root by starting the path with a slash:

string fileName= Server.MapPath("/Data/TextFile/someFile.txt");
Guffa
  • 640,220
  • 96
  • 678
  • 956