tags:

views:

96

answers:

3

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.

A: 

If I understand you right, you can just access it with:

"/portfolio"

The system will concatenate "/portfolio" to the url, if, for example it is "http://site.com" you will get "http://site.com/portfolio"

If you want the physical path, you can use

server.mappath("/portfolio")

and it will return the physical directory associated (for example E:\websites\site\portfolio")

tekBlues
+2  A: 

Calling Directory.GetCurrentDirectory() can be misleading when calling it from an ASP.NET app. The best way to do get around this is to use Server.MapPath() instead, which will allow you to map a web folder to its location on disk.

So for example, running this code:

string path = Server.MapPath("~/Portfolio");

will set 'path' equal to the location of that folder on disk.

If you're running this code from outside the context of a Page or control class, you won't be able to access the Server object directly. In this case, your code would look like this:

string path = HttpContext.Current.Server.MapPath("~/Portfolio");
Dan Herbert
A: 

the following piece of lines gets you to the 'bin' directory

Assembly asm = Assembly.GetExecutingAssembly();
String strAppHome = Path.GetDirectoryName(asm.CodeBase).Replace("file:\\", "");
davsan