The .NET Replacement for App.Path

A lot of confusion surrounds how to find out the startup path of your application —the .NET equivalent of the App.Path property we had in Visual Basic 6. I’ve personally written my own elongated routines, when in fact the solution is incredibly simple.

If you want to find out the application path of your Windows application, just reference the StartupPath property of the Application object, as so:

Dim strPath As String = Application.StartupPath 

Note that the returned path doesn’t include a trailing slash.

If you’re developing a class library or similar project, however, you might stumble upon a slight problem. You see, not all projects support the Application object. In these cases, you can use the System.Reflection class to analyze the executing assembly and return its location. A little like this:

Dim strPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location 

A bit more in depth, but still pretty darn simple

No comments:

Post a Comment