steventnorris
steventnorris

Reputation: 5876

Getting File Path in .NET From Code Behind

I would like to get the file path of the application and run a file from codebehind.

In my situation, I have a virtual directory set up with folders like [CSS,Javascript,AJAX,Images,SQLFiles]. In my App_Code folder, I have the .cs files for my codebehind. From those .cs files, I want to read a .sql file in the SQLFiles folder. How can I get the path of my virtual directory from the code behind in order to read the .sql file?

Upvotes: 2

Views: 4555

Answers (3)

CoolArchTek
CoolArchTek

Reputation: 3829

Use ApplicationPhysicalPath

var path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

Upvotes: 0

Sebastien H.
Sebastien H.

Reputation: 7116

you can use MapPath() to get your root directory.

HttpContext.Current.Server.MapPath("~") 

Upvotes: 3

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

You can use this:

Server.MapPath("~/SQLFiles/sqlfile.sql");

The ~ here indicates you want the virtual root, and then you're further saying I want to go a little further into SQLFiles. This will return you an absolute physical path.

Upvotes: 8

Related Questions