C # Seven methods for obtaining the current path,
Summary C # Seven methods for obtaining the current path
- C # The method for obtaining the current path is as follows:
- 1. System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName
- -Obtain the complete path of the module.
- 2. System. Environment. CurrentDirectory
- -Get and set the fully qualified directory of the current directory (the 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 I don't know what to return.
- 4. System. AppDomain. CurrentDomain. BaseDirectory
- -Obtain the base Directory of the program.
- 5. System. AppDomain. CurrentDomain. SetupInformation. ApplicationBase
- -Obtain and set the Directory Name of the application.
- 6. System. Windows. Forms. Application. StartupPath
- -Obtain the executable file path of 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.
- 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:
- 1 string path = "";
- 2
- 3 if (System. Environment. CurrentDirectory = AppDomain. CurrentDomain. BaseDirectory) // Windows applications are equal
- 4
- 5 ...{
- 6
- 7 path = AppDomain. CurrentDomain. BaseDirectory;
- 8
- 9}
- 10
- 11 else
- 12
- 13 ...{
- 14
- 15 path = AppDomain. CurrentDomain. BaseDirectory + "Bin \";
- 16
- 17}
- 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.
- 1. Server. MapPath
- 2. System. Windows. Forms. StartupPath
- 3. Type. Assembly. Location
- C # obtain the current path. method 2 can be applied to console applications, WinForm applications, and Windows Services. method 1 can be applied to Web applications, and method 3 can be applied.
- But method 3 is the path to load the application. For a Web application, the obtained path is: C: \ WINDOWS \ Microsoft. NET \ Framework \ v1.1.4322 \ Temporary ASP. NET Files directory. Therefore, we recommend that you use Server. MapPath for Web projects. Otherwise, method 2 is recommended. If you create a new class library. You can add a reference to System. Windows. Forms. StartupPath for use.
- C # The method for obtaining the current path is summarized here. I hope it will be helpful to you.
- C # How to obtain the current path of a program
- // 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)
- C # obtain the relative path
- 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 ");
- System. Environment. SystemDirectory; C:/windows/system32 directory
- 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
- 1 asp.net webform uses "Request. PhysicalApplicationPath to obtain the physical path of the virtual directory of the site, and finally contains" \ ";
- 2. c # Use winform
- A: "Application. StartupPath": gets the path of the directory where the current Application is located, and does not contain "\".
- B: "Application. ExecutablePath": obtains the path of the current Application file, including the file name;
- C: "AppDomain. CurrentDomain. BaseDirectory": gets the path of the directory where the current application is located, and finally contains "\";
- D: "System. Threading. Thread. GetDomain (). BaseDirectory": gets the path of the directory where the current application is located, and finally contains "\";
- E: "Environment. CurrentDirectory": gets the path of the current application and does not contain "\".
- F: "System. IO. Directory. GetCurrentDirectory": gets the path of the current application, and does not contain "\";
- 3. c # windows service
- Use "AppDomain. CurrentDomain. BaseDirectory"
- Or "System. Threading. Thread. GetDomain (). BaseDirectory ";
- Use "Environment. CurrentDirectory" and
- "System. IO. Directory. GetCurrentDirectory" will get the path of the "system32" Directory;
- If you want to use "Application. startupPath or Application. executablePath, You need to manually add the "System. windows. forms. dll. windows. forms "declares this reference;
- 4. Obtain the system installation directory from the uninstall program:
- System. Reflection. Assembly curPath = System. Reflection. Assembly. GetExecutingAssembly ();
- String path = curPath. Location; // obtain the path of the SetupLibrary file of the installer class. Obtain the directory where the file path is located to obtain the directory of the installer;
- 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
- C # Relative Path System Path
- // Obtain the path of the executable file that started the application, excluding the name of the executable file.
- String str5 = Application. StartupPath;
- // Obtain the file name of the currently executed exe.
- String str1 = Process. GetCurrentProcess (). MainModule. FileName;
- // Obtain and set the full path of the current directory (that is, the directory from which the process starts. The remarks are as defined. if the process is started locally or in the root directory of the network drive, the value of this attribute is the drive name followed by a backslash (for example, "C :\"). If the process is started in a subdirectory, the value of this attribute is a drive and subdirectory path (such as "C: \ mySubDirectory") without a backslash ").
- String str2 = Environment. CurrentDirectory;
- // Obtain the current working directory of the application.
- String str3 = Directory. GetCurrentDirectory ();
- // Obtain the base Directory, which is used by the Assembly conflict resolution program to detect the assembly.
- String str4 = AppDomain. CurrentDomain. BaseDirectory;
- // Obtain the path of the executable file that started the application, excluding the name of the executable file.
- String str5 = Application. StartupPath;
- // Obtain the path of the executable file that started the application, including the name of the executable file.
- String str6 = Application. ExecutablePath;
- // Obtain or set the name of the directory containing the application.
- String str7 = AppDomain. CurrentDomain. SetupInformation. ApplicationBase;
- // Example
- Application. StartupPath;
- // You can obtain F: \ learning \ c # Training \ win \ bin \ Debug.
- // Note that you need to add two \
- Application. StartupPath + "\ 3.jpg ";
- In c #, the relative path is represented by ".." and,
- "." Indicates the current directory,
- "..." Indicates the upper-level record.
- For example, if vs2005 is used to create a project named controls in the D: \ My Documents ents \ Visual Studio 2005 \ Projects directory, there is a controls folder in the Projects folder, the controls folder contains three files: controls. the GulfOfStLawrence folder in the sln controls folder.
- D: \ My Documents ents \ Visual Studio 2005 \ Projects \ Controls \ bin \ debugthis simple project can run the executable file controls.exe
- Now I want the path of the Gulf_of_St. _ Lawrence. mxd (arcgis desktop) project file under the D: \ My Documents \ Visual Studio 2005 \ Projects \ Controls \ GulfOfStLawrence folder.
- The relative path should be "... \ GulfOfStLawrence \ Gulf_of_St. _ Lawrence. mxd"
- That is, string filename = @ "... \ GulfOfStLawrence \ Gulf_of_St. _ Lawrence. mxd ";
- TIPS: 1. Using relative paths can increase project portability. It makes a project easier during the transplantation process, saving a lot of time for arranging project-related documents. (If the absolute path is set ).
- 2. Using relative paths also simplifies the program code
- 3. Note: (only relative paths can be used in the same drive (for example, all in D ).