Tuesday, May 27, 2008

GetPath in VB.NET 2005

In VB6 we used to use App.Path to get the path of the application.  When I converted to VB.NET 2003, I started to using
System.Reflection.Assembly.GetExecutingAssembly.Location without actually knowing what I was doing, but it worked and I was getting the location of the path,

However, in VB.NET 2005, this stopped working and the path it would give me was something like:
C:\Documents And Settings\......\LocalSettings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies\_ean0rwersws\.....

Now the "eanOrwersws" was a random folder that it generated whenever I rebuilt the application.  This seemed odd to me.

It turns out that the better way to get the Path of the application is to use one of the following calls:
System.Windows.Forms.Application.StartupPath <-- This returns the path without a "\"

I can't tell the difference between the following two calls, but they both return the path with a "\" at the end.
System.AppDomain.CurrentDomain.BaseDirectory()
AppDomain.CurrentDomain.SetupInformation.ApplicationBase()


No comments: