Getting dynamically web app name [or virtual dir name]

G

Guest

How can I retrieve web application name (or virtual dir name)?
(Actually I need physical root directory of web application)


I want to open some files in web application root relative
directory.
Previously I used to open the files when client connected and
thus could use the following code to retreive physical file location:

string webAppRootDir = Server.MapPath(Request.ApplicationPath);

Now I would like to open the files in application startup routine,
where Request object is not available.

when I call
string webAppRootDir = Server.MapPath("/");
I get c:\intepub\wwwroot (one level too "high")

when calling:
string webAppRootDir = Server.MapPath("./");
I get c:\intepub\wwwroot\appname\[somedirectory] (one [or more] level too
"deep")
Actually this only works if at application startup somebody typed in browser:
http://machinename/appName
(specifically application name has not been suffixed with nested dir name)

Is there any universal [working also without Request object]
way to get web application physical root path?


Note: I do not know virtual directory name, and must get it
dynamically via code (but do not know how). If I know this,
I might call the code:
string webAppRootDir = Server.MapPath("/"+virtDirName);


Thanks for help
Michal Januszczyk
 
C

Craig Deelsnyder

How can I retrieve web application name (or virtual dir name)?
(Actually I need physical root directory of web application)


I want to open some files in web application root relative
directory.
Previously I used to open the files when client connected and
thus could use the following code to retreive physical file location:

string webAppRootDir = Server.MapPath(Request.ApplicationPath);

Now I would like to open the files in application startup routine,
where Request object is not available.

when I call
string webAppRootDir = Server.MapPath("/");
I get c:\intepub\wwwroot (one level too "high")

when calling:
string webAppRootDir = Server.MapPath("./");
I get c:\intepub\wwwroot\appname\[somedirectory] (one [or more] level too
"deep")
Actually this only works if at application startup somebody typed in
browser:
http://machinename/appName
(specifically application name has not been suffixed with nested dir
name)

Is there any universal [working also without Request object]
way to get web application physical root path?


Note: I do not know virtual directory name, and must get it
dynamically via code (but do not know how). If I know this,
I might call the code:
string webAppRootDir = Server.MapPath("/"+virtDirName);


Thanks for help
Michal Januszczyk

Try
Server.MapPath("~")

~ maps to the root of the current application. It's an ASP.NET
thing...can also be used most of the time in web.config, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top