spud
spud

Reputation: 251

ASP.NET Server.MapPath not returning full path of a file

I have image files stored in "VS_Project\Resources\Images".

When I use the following code:

String str = Server.MapPath("a.png");

str becomes "VS_Project\a.png".

Why isn't it returning the full path?

Upvotes: 0

Views: 670

Answers (2)

James Lawruk
James Lawruk

Reputation: 31373

Try this:

string str = Server.MapPath("/") + "\\Resources\\Images\\a.png";

Upvotes: 0

Neil Knight
Neil Knight

Reputation: 48587

You need a / before the a.png. 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 .asp file being processed.

Upvotes: 2

Related Questions