2

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?

3 Answers 3

8

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.

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

Comments

3

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

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

Comments

0

Use ApplicationPhysicalPath

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.