1. Obtain the path of the current file
1. System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName
Obtain the complete path of the module, including the file name.
2. System. Environment. CurrentDirectory
Obtain and set the fully qualified directory of the current directory from which the process starts.
3. System. IO. Directory. GetCurrentDirectory ()
Obtain the current working directory of the application. This is not necessarily the directory from which the program starts. It may be stored in C: \ www. This function may return C: \ Documents ents and Settings \ ZYB \, or C: \ Program Files \ Adobe \, sometimes not necessarily return anything. This is the last operation directory of any application. For example, if you open the file E: \ doc \ my.doc in Word, then, E: \ doc is returned.
4. System. AppDomain. CurrentDomain. BaseDirectory
Obtain the base Directory of the program.
5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase
Obtain and set the name of the directory containing the application.
6. System. Windows. Forms. Application. StartupPath
Obtain the path of the executable file that started the application. The effect is the same as that of 2 and 5. Only 5 returns an extra "\" behind the string.
7. System. Windows. Forms. Application. ExecutablePath
Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.
Ii. Operating Environment Variables
The System. Environment. GetEnvironmentVariable () method can be used to conveniently obtain System Environment variables, such:
System. Environment. GetEnvironmentVariable ("windir") to obtain the path of the windows System directory.
The following are common values of environment variables:
System. Environment. GetEnvironmentVariable ("windir ");
System. Environment. GetEnvironmentVariable ("INCLUDE ");
System. Environment. GetEnvironmentVariable ("TMP ");
System. Environment. GetEnvironmentVariable ("TEMP ");
System. Environment. GetEnvironmentVariable ("Path ");
Finally, I posted the variable values obtained through the above operations. In advance, I wrote a WinForm program. The project file is stored in D: \ Visual Studio Projects \ MyApplication \ LifeAssistant, the compiled file is in D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug. The final result is as follows:
1. System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug \ LifeAssistant.exe
2. System. Environment. CurrentDirectory = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug
3. System. IO. Directory. GetCurrentDirectory () = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug
4. System. AppDomain. CurrentDomain. BaseDirectory = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug \
5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug \
6. System. Windows. Forms. Application. StartupPath = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug
7. System. Windows. Forms. Application. ExecutablePath = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug \ LifeAssistant.exe
System. Environment. GetEnvironmentVariable ("windir") = C: \ WINDOWS
System. Environment. GetEnvironmentVariable ("INCLUDE") = C: \ Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ v1.1 \ include \
System. Environment. GetEnvironmentVariable ("TMP") = C: \ javase ~ 1 \ zhoufoxcn \ LOCALS ~ 1 \ Temp
System. Environment. GetEnvironmentVariable ("TEMP") = C: \ police ~ 1 \ zhoufoxcn \ LOCALS ~ 1 \ Temp
System. environment. getEnvironmentVariable ("Path") = C: \ WINDOWS \ system32; C: \ WINDOWS \ System32 \ Wbem; C: \ jdk1.5.0 \ bin; C: \ MySQLServer5.0 \ bin; C: \ Program Files \ Symantec \ pcAnywhere \; C: \ Program Files \ Microsoft SQL Server \ 80 \ Tools \ BINN
For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code:
String path = "";
If (System. Environment. CurrentDirectory = AppDomain. CurrentDomain. BaseDirectory) // Windows applications are equal
{
Path = AppDomain. CurrentDomain. BaseDirectory;
}
Else
{
Path = AppDomain. CurrentDomain. BaseDirectory + "Bin \";
}
In this way, if we write a class library and use Assembly. LoadFrom in the class library, because it is a general class library, it may also be used in Windows programs, so it is very convenient to use the above Code.