C # Notes for getting the program path (version 1)
You can obtain the path of a running program in the following seven ways:
- System. Diagnostics. process. getcurrentprocess (). mainmodule. filename
The module File Name is obtained. If the module File Name is obtained in the debugging environment of vs2008, the complete file name of the program name.vshost.exe is obtained.
- System. environment. currentdirectory
System. Io. Directory. getcurrentdirectory ()
The two methods obtain the same path and obtain the current path, which is not necessarily the path of the program. Any method that changes the current path will change it. For example, openfiledialog changes it every time the directory is changed. Therefore, using these two methods to obtain the program path is not reliable.
- System. appdomain. currentdomain. basedirectory
System. appdomain. currentdomain. setupinformation. applicationbase
These two methods are reliable methods for obtaining the program path. The end of the returned path is. You can easily add any path to the backend. Of course, even if not, you can use path. Combine to merge paths to obtain the desired path.
- System. Windows. Forms. application. startuppath
This method is also a reliable method for obtaining the program path. However, the end of the returned path does not end. In addition, it is the form namespace. Therefore, it is better to use WPF.
- System. Windows. Forms. application. executablepath
This method obtains the complete file name of the execution program. Is the most reliable way, you only need to remove the program file name to obtain the path.
Version 2:
// Obtain the complete path of the new process component and associate it with the active process, including the file name (process name ).
String STR = system. Diagnostics. process. getcurrentprocess (). mainmodule. filename;
Result: X: \ XXX \ xxx.exe (.exefile directory. .exe file name)
// Obtain and set the full path of the current directory (that is, the directory from which the process starts.
String STR = system. environment. currentdirectory;
Result: X: \ XXX (directory of the .exe file)
// Obtain the base Directory of the current application domain of the current thread, which is used by the Assembly conflict resolution program to detect the assembly.
String STR = system. appdomain. currentdomain. basedirectory;
Result: X: \ XXX \ (directory where the .exe file is located + "\")
// Obtain and set the name of the directory containing the application.
String STR = system. appdomain. currentdomain. setupinformation. applicationbase;
Result: X: \ XXX \ (directory where the .exe file is located + "\")
// Obtain 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 (directory of the .exe file)
// Obtain 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.exe (.exefile directory. .exe file name)
// Obtain the current working directory of the application (unreliable ).
String STR = system. Io. Directory. getcurrentdirectory ();
Result: X: \ XXX (directory of the .exe file)
Http://www.cnblogs.com/hantianwei/archive/2009/11/04/1595891.html