SoFunction
Updated on 2025-03-06

Detailed summary of C# file path operation

1. Get the path to the current file
1. ().
Gets the full path to the module, including the file name.

2.
Gets and sets a fully qualified directory for the current directory (the directory from which the process starts).

3. ()
Gets the current working directory of the application. This is not necessarily the directory from which the program starts. It is possible that the program is placed in C:\www. This function may return C:\Documents and Settings\ZYB\, or C:\Program Files\Adobe\. Sometimes it does not necessarily return something. This is the directory that any application has operated for the last time. For example, if you open the E:\doc\ file in Word, then execute this method and return E:\doc.

4.
Get the base directory of the program.

5.
Gets and sets the name of the directory that includes the application.

6.
Gets the path to the executable file that started the application. The effect is the same as 2 and 5. It's just that there is an "\" after the string returned by 5.

7.
Get the path and file name of the executable file that started the application, the effect is the same as 1.

2. Operating environment variables
Using the () method, you can easily obtain system environment variables, such as:
("windir") can obtain the path to the Windows system directory.

The following are some commonly used environment variable values:

("windir");
("INCLUDE");
("TMP");
("TEMP");
("Path");

Finally, I posted the variable values ​​I obtained from the above operation. In advance, I wrote a WinForm program, and the project files are stored in D:\Visual Studio Projects\MyApplication\LifeAssistant. The compiled file is located in D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug. The final result is as follows:

1、 ().=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

2、=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

3、()=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

4、=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

5、 =D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

6、=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

7、 =D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

("windir")=C:\WINDOWS
("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
("TMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
("TEMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp
("Path")=C:\WINDOWS\system32;C:\WINDOWS;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