How to determine where an application is installed

Screenshot showing a messagebox describing where the application is installed on the filesystemMost Windows Mobile software is installed via the use of a CAB file. On recent versions of the Windows Mobile operating system CAB Files offer the user the choice of where to install the application. However numerious system APIs and features require the full path to the application’s executable to be specified.

This blog entry discusses how a .NET Compact Framework application can determine its installation location for use in such APIs.

How to determine the full path to the executable

using System.Reflection;
private string GetApplicationExe()
  // Determine the full path to the application executable
  return Assembly.GetExecutingAssembly().GetName().CodeBase;

The GetExecutingAssembly method returns an Assembly object that represents the Assembly that contains the code which called the GetExecutingAssembly method. Hence if this call is placed within code contained within the main executable it will return the path to the main executable.

An example return value may be “\program files\foobar\foobar.exe”. This value is useful to have for APIs such as SystemState.EnableApplicationLauncher which expect the full path to our executable to be specified. By not hard coding the path within our application we allow the end user to move the application around on their device without breaking the application’s functionality.

How to determine the directory containing the application
It is common for an application to install a number of configuration or data files required by the application. Since Windows CE does not have the concepts of a current working directory or relative paths full paths need to be specified when attempting to access these files.

By using the Path.GetDirectoryName static method we can extract the directory component of the path obtained above, so that we do not need to hard code the location of our support files.

using System.IO;
private string GetApplicationPath()
  // Return the full path to the directory our
  // application is installed within.
  string codebase = GetApplicationExe();
  return path.GetDirectoryName(codebase);

A nice helper method to include within an application is one which takes a name of a file (or subdirectory) which should be located within our application directory and returns a full path to that file.

string GetPathToApplicationFile(string filename)
  string path = GetApplicationPath();
  return Path.Combine(path, filename);

For example GetPathToApplicationFile(@”config\foo.txt”); would return a string similiar to “\Program Files\TestApp\config\foo.txt” where \Program Files\TestApp is the directory the application is currently installed in.

Sample Project

[Download - 11.8KB]

A small sample project is available for download. When the application is started it displays a Message box that displays the location of the executable. Notice that the application correctly determines if the CAB file has been installed on to a storage card or the main memory of the device. It will also cope with the application files manually being moved to a different location.

The CAB file also installs two text files within the application directory. By using the main menu you can load one of these files and display its contents within the window.

Leave a Reply