Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Current virtual directory from Application_Start ?

9 views
Skip to first unread message

Saar Carmi

unread,
Aug 15, 2005, 5:22:20 AM8/15/05
to
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.

Saar Carmi

unread,
Aug 15, 2005, 5:22:33 AM8/15/05
to

Andrea Zani

unread,
Aug 15, 2005, 5:44:28 AM8/15/05
to
Saar Carmi <saar...@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


Juan T. Llibre

unread,
Aug 15, 2005, 6:19:37 AM8/15/05
to
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" <and...@aspitalia.com> wrote in message
news:0uZLe.17346$F23.2...@twister2.libero.it...

Juan T. Llibre

unread,
Aug 15, 2005, 7:09:56 AM8/15/05
to
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...
======================

Andrea Zani

unread,
Aug 15, 2005, 7:38:36 AM8/15/05
to
Juan T. Llibre <nomail...@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)

Juan T. Llibre

unread,
Aug 15, 2005, 8:36:43 AM8/15/05
to
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" <and...@aspitalia.com> wrote in message

news:09%Le.18204$HM1.4...@twister1.libero.it...

Andrea Zani

unread,
Aug 15, 2005, 12:26:28 PM8/15/05
to
"Juan T. Llibre" <nomail...@nowhere.com> ha scritto nel messaggio
news:OxKmsnYo...@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!

0 new messages