Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current directory in asp.net mvc

Tags:

I am trying to construct a file path in order to read an XSLT file, like so:

string path = "../_xslt/example.xslt"; StreamReader reader = new StreamReader(path);  

...where I am in a controller (/Controllers/ExampleController.cs), and the '/_xslt/' folder is at the same level as '/Controllers'

However, the error I am getting is:

(System.IO.DirectoryNotFoundException) Could not find a part of the path 'c:\windows\system32\_xslt\example.xslt'.

Am I going about this the wrong way?

Thanks for any help!

like image 429
Michael S Avatar asked Feb 22 '12 11:02

Michael S


People also ask

How do I get the current directory in VB net?

You can use WinDir variable to get the current directory. The Environment. GetEnvironmentVariable method can be used for that. Here is the code snippet that gets the current directory using VB.NET.

How do I get the current directory in bash?

By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.


1 Answers

You can use the HttpServerUtility.MapPath method to map any relative paths for you, in your controller this is easily accessible via the ControllerContext:

string path = ControllerContext.HttpContext.Server.MapPath("~/_xslt/example.xslt"); ... 
like image 136
Rich O'Kelly Avatar answered Oct 09 '22 13:10

Rich O'Kelly