Get Application Directory [C#]
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);
Files and Folders Examples
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Read Text File – how to read lines from text file
- [C#] Load Text File to String – how to load text from file to string
- [C#] Get Files from Directory – how to get files from directory
- [C#] Delete All Files – how to delete files from the specified directory
- [C#] Get Application Directory – how to get application or assembly folder
- [C#] File Attributes – how to get or set file attributes
- [C#] Get File Time – how to get last modification time of a file
- [C#] Open File With Associated Application – how to launch the default application
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