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.[C#]
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.[C#]
using System.IO; using System.Reflection; string path = Path.GetDirectoryName( Assembly.GetAssembly(typeof(MyClass)).CodeBase);
See also
- Application.ExecutablePath – MSDN – returns path of application .exe file
- Assembly.GetAssembly – MSDN – returns assembly for the specified class
- Path.GetDirectoryName – MSDN – returns the dir part of the specified path
Комментариев нет:
Отправить комментарий