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 cho开发者_运维百科ose 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?
In runtime, you can use:
Application.StartupPath
Application.ExecutablePath
that will tell you where your .exe is. Hope that helps.
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.
Like this:
Shared ReadOnly AppDirectory As String = _
Path.GetDirectoryName(New Uri(GetType(Program).Assembly.CodeBase).LocalPath)
You can have a look at
Application.ExecutablePath Property
or
AppDomain.BaseDirectory Property
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)
精彩评论