Summary of the Method of Obtaining Program Path by C Implementation

  • 2021-07-24 11:40:44
  • OfStack

In this paper, an example is given to describe the method of obtaining program path by C #. Share it for your reference. The details are as follows:

Get the directory of DLL:


Assembly myAssembly = Assembly.GetEntryAssembly();
string path = myAssembly.Location;
DirectoryInfo dr = new DirectoryInfo(path);
path=dr.Parent; // On the current directory 1 Level directory 

C # Method to get the current path of the program:


System.Environment.CurrentDirectory;
// Example : c:\test
Application.ExecutablePath;( Including name) 
// Example : c:\test\myapp.exe
Application.StartupPath; (Excluding names) 
// Example : c:\test
// Gets the new  Process  Component and associates it with the current active process, including the file name ( Process name ) . 
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe Directory where the file is located +.exe Filename )
// Gets and sets the fully qualified path to the current directory (that is, the directory from which the process started). 
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe Directory where the file is located )
// Gets the current  Thread  The base directory of the current application domain of, which is used by the assembly resolver to probe assemblies. 
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe Directory where the file is located +"\")
// Gets and sets the name of the directory containing the application. 
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe Directory where the file is located +"\")
// Gets the path of the executable file that started the application, excluding the name of the executable file. 
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe Directory where the file is located )
// Gets the path of the executable file that started the application, including the name of the executable file. 
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe Directory where the file is located +.exe Filename )
// Gets the current working directory of the application ( Unreliable ) . 
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe Directory where the file is located )

Get the system special folder path (favorites, desktop)

1. Favorites path

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites)

2. Desktop path
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)

I hope this article is helpful to everyone's C # programming.


Related articles: