There are some questions about how to get application path or executable path in code for an application. This article answers those questions.
Application Path
CommonPathAppData property returns the path for
the application data that is shared
among all users. If a path does
not exist, the default created path
format is Base Path\CompanyName\ProductName\ProductVersion.
UserAppDataPath property returns the path for
the application data of a user. If a path does
not exist, the default created path
format is Base Path\CompanyName\ProductName\ProductVersion.
LocalUserAppDataPath property returns the path for
the application data of a local user.
If a
path does not exist, the default created path format is Base
Path\CompanyName\ProductName\ProductVersion.
StartupPath property returns the path for
the executable file that started
the application, not the path where the application executable is
stored.
ExecutablePath property returns the path for
the executable file that started
the application, including the executable name.
CommonAppDataRegistry property returns the registry
key for the application data
that is shared among all users.
AllowQuit property returns true if the
caller can quit the current
application.
The C# code
snippet in Listing 1 uses the above discussed properties to add the
values of these
properties to a ListBox control on a Form.
listBox1.Items.Add("CommonAppDataPath: " + Application.CommonAppDataPath);
listBox1.Items.Add("CommonAppDataRegistry: " + Application.CommonAppDataRegistry.ToString());
listBox1.Items.Add("ExecutablePath: " + Application.ExecutablePath);
listBox1.Items.Add("LocalUserAppDataPath: " + Application.LocalUserAppDataPath);
listBox1.Items.Add("StartupPath: " + Application.StartupPath);
listBox1.Items.Add("UserAppDataPath: " + Application.UserAppDataPath);
Listing 2
The output of
Listing 2 looks like Figure 2.
Figure 2
Listing 3 is
the VB.NET version of Listing 2.
listBox1.Items.Add("CommonAppDataPath: " + Application.CommonAppDataPath)
listBox1.Items.Add("CommonAppDataRegistry: " + Application.CommonAppDataRegistry.ToString())
listBox1.Items.Add("ExecutablePath: " + Application.ExecutablePath)
listBox1.Items.Add("LocalUserAppDataPath: " + Application.LocalUserAppDataPath)
listBox1.Items.Add("StartupPath: " + Application.StartupPath)
listBox1.Items.Add("UserAppDataPath: " + Application.UserAppDataPath)
Listing 3