469,648 Members | 1,166 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Current virtual directory from Application_Start ?

Hi

How can I get the application's virtual directory from the
Application_Start method in Global.asax ?

Keep in mind there is no request/response object available yet in this
stage.

Thanks
Saar.

Nov 19 '05 #1
6 5597
Saar Carmi <sa*******@gmail.com> ha scritto:
Hi

How can I get the application's virtual directory from the
Application_Start method in Global.asax ?
AppDomain.CurrentDomain.BaseDirectory
Thanks
Saar.


--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
Nov 19 '05 #2
re:
AppDomain.CurrentDomain.BaseDirectory
Nah.

That will return the physical directory, not the virtual dir.


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Andrea Zani" <an****@aspitalia.com> wrote in message
news:0u********************@twister2.libero.it... Saar Carmi <sa*******@gmail.com> ha scritto:
Hi

How can I get the application's virtual directory from the
Application_Start method in Global.asax ?


AppDomain.CurrentDomain.BaseDirectory
Thanks
Saar.


--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az

Nov 19 '05 #3
You have to get really tricky to get this, Saar, because you can't
retrieve the ServerVariables in the Application_Start global.asax
event, as you correctly point out, because the Request object
is not yet available.

You can kludge the retrieval of the virtual directory by splitting
AppDomain.CurrentDomain.DynamicDirectory and adding a "/".

In global.asax :

Sub Application_Start()
Dim MyArray() As String = Split(AppDomain.CurrentDomain.DynamicDirectory, "\")
Application("App_Virtual_Dir") = ("/" & MyArray(6).ToString())
End Sub

In your retrieval page :

<%@ Page Language="VB" EnableViewState="false" %>
<script runat="server">
Sub Page_Load(obj as object, e as eventargs)
Dim AppDir as String = Application("App_Virtual_Dir")
lblMessage.Text = AppDir
End Sub
</script>
<html>
<body>
<asp:Label id="lblMessage" runat="server"/></asp:Label> <br />
</body>
</html>

---000---

This works because AppDomain.CurrentDomain.DynamicDirectory
has a fixed path which includes the virtual directory name, which will
always be in the 7th spot. ( Remember, VB.NET uses zero-based Arrays. )

You could, alternately, split AppDomain.CurrentDomain.BaseDirectory
and, if your physical directory is named the same as your virtual directory,
you could retrieve the name and prepend a forward slash to compose your
virtual path.


Splitting AppDomain.CurrentDomain.BaseDirectory would need trial and error,
though, because the physical directory may be deep in the wwwroot directory
tree, or even outside the wwwroot tree, and the number of subdirectories might
vary, while the AppDomain.CurrentDomain.DynamicDirectory's path is fixed
and the MyArray(6) position will always return just the name of the virtual
directory, needing only the prepended "/" to complete the virtual path.
Good luck !


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Saar Carmi" <sa*******@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com...
Hi

How can I get the application's virtual directory from the
Application_Start method in Global.asax ?

Keep in mind there is no request/response object available yet in this
stage.

Thanks
Saar.

Nov 19 '05 #4
Juan T. Llibre <no***********@nowhere.com> ha scritto:
re:
AppDomain.CurrentDomain.BaseDirectory


Nah.

That will return the physical directory, not the virtual dir.


Ops, :)

For read the virtual dir:
dim path as string=AppDomain.CurrentDomain.FriendlyName
path=path.Substring(path.LastIndexOf("/"))
path=path.Substring(0,path.IndexOf("-"))
Response.write(path)

--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
Nov 19 '05 #5
That's better... ;-)

I also wrote a working kludge.
Yours is a bit more elegant, though.

Thanks.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Andrea Zani" <an****@aspitalia.com> wrote in message
news:09********************@twister1.libero.it...
Juan T. Llibre <no***********@nowhere.com> ha scritto:
re:
AppDomain.CurrentDomain.BaseDirectory


Nah.

That will return the physical directory, not the virtual dir.


Ops, :)

For read the virtual dir:
dim path as string=AppDomain.CurrentDomain.FriendlyName
path=path.Substring(path.LastIndexOf("/"))
path=path.Substring(0,path.IndexOf("-"))
Response.write(path)

--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az

Nov 19 '05 #6
"Juan T. Llibre" <no***********@nowhere.com> ha scritto nel messaggio
news:Ox**************@TK2MSFTNGP14.phx.gbl...
Sub Application_Start()
Dim MyArray() As String = Split(AppDomain.CurrentDomain.DynamicDirectory,
"\")
Application("App_Virtual_Dir") = ("/" & MyArray(6).ToString())
End Sub


Good solution!

--
AZ [Microsoft - .NET MVP]
Mia Home page: http://ciclismo.sitiasp.it
Asp.Net community: http://www.aspitalia.com
Il mio blog: http://blogs.aspitalia.com/az
Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Leszek | last post: by
reply views Thread by Saar Carmi | last post: by
2 posts views Thread by Alwin D | last post: by
6 posts views Thread by Scott M. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.