473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 5829
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeffry van de Vuurst | last post by:
Hi, (sorry for the crosspost, I wasn't sure which was the best place to put this). I was just thinking about something and wondered if any of you has some ideas about this. I'm using the...
3
by: PHaroZ | last post by:
Hi, I want to retrieve the complete full path to the directory of my current page but i don't find how to do that. For example i want : D:\myWebSite\firstDotNetWebApp\dir1\ I tried...
5
by: Leszek | last post by:
Hello, Could anybody explain what's a difference between a virtual directory and an application root under IIS? I'm a little bit confused. This is mu problem: Let's assume the following...
3
by: Nathan Phelps | last post by:
I realize that I could find my virtual directory using some of the methods in the HttpRequest object, but since the HttpRequest object is not in context during the Application_Start event, I'm at a...
0
by: Saar Carmi | last post by:
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...
14
by: gio | last post by:
I have a problem and the solution should works under windows and unix OS. Suppose I have a program ex.c in the directory X (so the current working directory of ex.c is X). Also suppose I have...
2
by: Alwin D | last post by:
Hello, How can I get the current directory of Web Service? Usually my Web services are under Inetpub directory. I would like to write trace file into that place. If I try to trace e.g....
1
by: KUTTAN | last post by:
I want to get the current applications virtual path in global.ascx (event Application_Start()) How can I access that ? Request.ApplicationPath is not valid there :(
6
by: Scott M. | last post by:
I didn't get a resolution to this in my earlier post, so I'll try again: System: Windows XP Pro. (SP2) with IIS installed and running PRIOR to VS 2008 Pro. installation. VS 2008 Pro. (full...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.