Tuesday, September 18, 2012

C# Get Assembly Location Path

Often when developing different applications or services you need to know where is actually your exe or a dll located. For example, you developing a service which will create some files and directories in the same place where the service is running, so you need to know somehow in the run time the path to your service dllFortunately for these purposes we have Reflection mechanism in C#. 
First, include reflection library in your code:
using System.Reflection;

Now, to get the path to the assembly in the run time - use this command:
string assemblyPath = Assembly.GetAssembly(typeof(ClassName)).Location;

Or if you need the path to the assembly directory then you can apply this code:
string directoryPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(ClassName)).Location);

No comments:

Post a Comment