Software Development

Locate the executable path using VB.NET

In this Visual Basic .NET tip, Irina Medvinskaya looks at a simple way to determine the path where the current executable resides.

It is often useful to be able to determine the path where the current executable resides. A simple way to do this using Visual Basic .NET is to read the value of the StartUpPath property of the Application class. This can also be achieved by determining the current working path or utilizing reflection to determine the code base location of the currently executing assembly.

Example

In order to demonstrate finding the executable path, add the listbox control ListBox1 to the form and add the code in Listing A. (Note: You must use theSystem.Windows.Forms namespace to use the technique in this example.) When you run the code, your result will look like Figure 1.

In this example, I utilize the System.Windows.Forms namespace and initially get the ExecutablePath property of the Application class. The value is displayed in ListBox1. Then, I get the StartUpPath property of the Application class and also display it in ListBox1.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

4 comments
pweegar
pweegar

In order to get this example to work in VB Net 2003, the subroutine must be called. Calling it from the load event works. Geez, wish the author would test code before it's published.

ChuckSomerville
ChuckSomerville

For those of us still doing work with VB6 (and there are a lot of us), here's how to do this in VB6. In the (general) (declarations) section of your code, declare the following Windows API call... Private Declare Function GetModuleFileName _ Lib "kernel32" Alias "GetModuleFileNameA" _ (ByVal hModule As Long, _ ByVal lpFileName As String, _ ByVal nSize As Long) As Long (of course on fewer lines, if you like) and then in the portion of code where you need to get the name, add this... Dim hProcess As Long, nSize As Long, sFileName As String Dim i as integer, strlen As Long sFileName = String(255, Chr(0)) nSize = 250 hProcess = App.hInstance strlen = GetModuleFileName(hProcess, sFileName, nSize) ' sFileName is null-terminated c-style string i = InStr(sFileName, Chr(0)) sFileName = Left(sFileName, i - 1) ...your fully-qualified .EXE name is in sFileName. So far, I haven't found anything I can't do in VB6. Here's an excellent reference... Visual Basic Programmer's Guide to the Win32 API Dan Appleman Sams Publishing, 1999 ISBN 0-672-31590-4 US: http://tinyurl.com/ybhrbt UK: http://tinyurl.com/t5sao Chuck Somerville Eastman Kodak Company Dayton Ohio

pmitchell
pmitchell

The code above actually returned the full path and the filename of the running application. I made a tweak to it, which returned only the path, not the name of the file as well. On the line that reads: i = InStr(sFileName, Chr(0)) change to: i = InStrRev(sFileName, Chr(92)) This will hunt the sFileName string for the last instance of Chr(92), which is a backslash, then the code returns everything before that backslash. Saving a file then means saving as sFileName & "\filename.txt"