tags:

views:

40

answers:

6

i published an application in vb.net. the user will be able to install the application anywhere they choose on the computer (or perhaps not anywhere they choose but where ever the default location is). how can i programmatically get the location where the user installed the application? another words i need the application to know where it is running from. how do i detect that?

+4  A: 

In runtime, you can use:

Application.StartupPath
M.A. Hanin
+2  A: 

Like this:

Shared ReadOnly AppDirectory As String = _
     Path.GetDirectoryName(New Uri(GetType(Program).Assembly.CodeBase).LocalPath)
SLaks
what do u think of Hanin's answer?
I__
It's much shorter, and will still work.
SLaks
+2  A: 
Application.ExecutablePath 

that will tell you where your .exe is. Hope that helps.

Jim
+2  A: 

You can have a look at

Application.ExecutablePath Property

or

AppDomain.BaseDirectory Property

astander
+3  A: 

If your app is a Windows Forms app you can use the Application static class, as others have noted. For other kinds of applications, use reflection:

Dim a = System.Reflection.Assembly.GetEntryAssembly()
Dim location = a.Location

I had to do this the other day, works great.

magnifico
Location returns the full path AND file name. Call Path.GetDirectoryName to get just the path.
gbogumil
@gbogumil Yep...
magnifico
+2  A: 

If you put this code in your exe then it will give you the path of the exe.

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
gbogumil