2

In my asp.net web application,I have to read somef file inside the App_Data folder,but I have no idea hot to set the path.

If in the web form,I know I can use the:

Sever.mapPath("~/App_Data");

But now I want to retrive the path in the .cs file in the App_code.

So there is no HttpServer context there.

Any idea?

2 Answers 2

13

Use HostingEnvironment.MapPath instead.

Sign up to request clarification or add additional context in comments.

Comments

3

In asp.net have System.IO namespace which contain many functions related to files.

you can get both path and file related function in below namespace

System.IO.Path
System.IO.File

you can get full file path in .cs file as below

string strFullPath = System.IO.Path.GetFullPath(Server.MapPath("~/App_Code/Class1.cs"));    

Hope this will helps you...

1 Comment

I'm not sure how this is the answer. hguser asked how to get the path when there is no Server object/property, but that's what you used in the answer. Simon Svensson's answer is correct: use HostingEnvironment instead of Server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.