How do I search a sub directory in my ASP.Net web site?
I know if this was a regular Winform project, I could easily get it by the following:
Environment.CurrentDirectory
or
System.IO.Directory.GetCurrentDirectory()
then
string[] directories = System.IO.Path.Combine(Environment.CurrentDirectory, "Portfolio")
How do I build the path to a subfolder called Portfolio in ASP.Net?
I’m having a problem building everything from the http://??????????/Portfolio. How do I find the ?????? part?
I tried the code above but got a completely different directory…
I don’t want to hard code everything before that last subfolder because it will be different on another server.
Calling
Directory.GetCurrentDirectory()can be misleading when calling it from an ASP.NET app. The best way to do get around this is to useServer.MapPath()instead, which will allow you to map a web folder to its location on disk.So for example, running this code:
will set ‘
path‘ equal to the location of that folder on disk.If you’re running this code from outside the context of a
Pageor control class, you won’t be able to access theServerobject directly. In this case, your code would look like this: