.NET 1.1+Getting the Current Assembly's Folder
When you obtain the current working folder for an executing program, this may be a different location to the directory in which the program's assemblies are stored. This article explains how to find the physical location of the current assembly's file.
Assembly Class
The Assembly class is a standard class within the System.Reflection namespace. It represents an assembly and provides access to the metadata that is associated with that assembly. The class also includes some static methods that allow a new Assembly object to be created and populated with information.
As the Assembly class is found in the System.Reflection namespace, the code is simplified if the following using directive is added to the files where the class is in use:
Obtaining the Current Assembly's Location
The location of an assembly can be obtained from an Assembly object using the Location property. This property returns a string containing the path of the assembly. This may be a simple file path or a UNC path, depending upon the location.
To find the current assembly's location, you must first populate an Assembly object with the details of the executing assembly. This is achieved using the static GetExecutingAssembly method. The Location property is then made available. In the following sample code, the path to the executing assembly is assigned to the "location" variable.
Assembly currentAssembly = Assembly.GetExecutingAssembly();
string location = currentAssembly.Location;
31 October 2008