Following examples show how to get application or assembly folder.
Directory of windows forms application (.exe)
Class Application in System.Windows.Forms namespace has static property ExecutablePath. It contains path of the .exe file (that started the application) including the executable file name. To get only the folder part of the path, use static method GetDirectoryName of Path class.
using System.IO;
using System.Windows.Forms;
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
Directory of any loaded assembly (.exe or .dll)
First get reference to the assembly. You can use static methods of Assembly class. To get assembly of currently executing code use method Assembly.GetExecutingAssembly. To get assembly in which the specified class is defined use method Assembly.GetAssembly (with the specified class type as a paramater). The assembly must be loaded. Next get assembly file path using Assembly.CodeBase property.
using System.IO;
using System.Reflection;
string path = Path.GetDirectoryName(
Assembly.GetAssembly(typeof(MyClass)).CodeBase);
resource from http://www.csharp-examples.net/
0 comments:
Post a Comment