Current virtual directory from Application_Start ?

S

Saar Carmi

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.
 
A

Andrea Zani

Saar Carmi said:
Hi

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

Thanks
Saar.
 
J

Juan T. Llibre

re:
AppDomain.CurrentDomain.BaseDirectory

Nah.

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






Andrea Zani said:
Saar Carmi said:
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
 
J

Juan T. Llibre

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 !
 
A

Andrea Zani

Juan T. Llibre said:
re:

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)
 
J

Juan T. Llibre

That's better... ;-)

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

Thanks.





Andrea Zani said:
Juan T. Llibre said:
re:

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
 
A

Andrea Zani

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

Good solution!
 

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

Members online

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top