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

12

Use HostingEnvironment.MapPath instead.

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
  • 4
    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.
    – Alan McBee
    Nov 11, 2013 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.