The process object is represented by the System.Diagnostics.Process class in .NET. The file name of the currently executing exe can be obtained by calling Process.GetCurrentProcess().MainModule.FileName. But this method only gets the file name. If the working directory is not switched during the program running, you can call the System.IO.Path method to obtain the absolute path. However, the current directory can also be obtained through Environment.CurrentDirectory, and many software will switch the working directory when using Open Dialog to open a file, making this mechanism ineffective.
If it is in a Windows Forms application, the current application is also represented as a System.Windows.Forms.Application object. Through its static properties Application.ExecutablePath and Application.StartupPath, the path and startup path of the executable file can be obtained.
But if it is not in a Windows application or in a Library, even if the properties of the Application object can still be obtained, you still need to add a reference to the Assembly System.Windows.Forms in the project, which is very inconvenient. At this time, you can obtain the currently executing Assembly through the Assembly's static method, GetCallingAssembly or GetExecutingAssembly, and then obtain the location of the assembly through the Location of the Assembly class.
However, when using Assembly, you may encounter permission problems. At the same time, Assembly.GetCallingAssembly or Assembly.GetExecutingAssembly may not get the location of the .exe file. The assembly with a strong name added to the GAC does not need to be in the same directory as the .exe when running.
When the .NET process starts, an AppDomain will be created, and all Assembly will be loaded into a certain AppDomain. The SetupInformation attribute is provided in the AppDomain, which can obtain some information when the AppDomain is started. Therefore, you can call AppDomain.CurrentDomain.SetupInformation. .ApplicationBase gets the path where the current application is located.
After obtaining the required directory through the above method, you can call the System.IO.Path method to obtain the file name, directory name, absolute path, etc. Stop parsing path strings and use the System.IO.Path class instead.
When developing applications under .NET, understanding the relationship between Process/Application->AppDomain->Assembly is very helpful for implementing correct logic.