Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting File Path in .NET From Code Behind

Tags:

c#

.net

io

asp.net

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?

like image 544
steventnorris Avatar asked May 04 '26 02:05

steventnorris


2 Answers

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.

like image 190
Mike Perrenoud Avatar answered May 05 '26 15:05

Mike Perrenoud


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

HttpContext.Current.Server.MapPath("~") 
like image 20
Sebastien H. Avatar answered May 05 '26 14:05

Sebastien H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!