C file path operations are summarized in detail

  • 2020-06-03 08:09:16
  • OfStack

1. Get the path to the current file
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
Gets the full path of the module, including the file name.

2. System.Environment.CurrentDirectory
Gets and sets the fully qualified directory of the current directory from which the process started.

3. System.IO.Directory.GetCurrentDirectory()
Gets the current working directory of the application. This function may return C:\Documents and \ZYB\, or C:\Program Files\Adobe\, sometimes not 1 return something, this is the last operation of any application directory, such as you use Word opened E:\doc\ my.doc this file, This method returns E:\doc.

4. System.AppDomain.CurrentDomain.BaseDirectory
Gets the base directory of the program.

5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
Gets and sets the name of the directory that contains the application.

6. System.Windows.Forms.Application.StartupPath
Gets the path to the executable that started the application. 2. 51 samples. 5 just returns an extra "\" after the string

7. System.Windows.Forms.Application.ExecutablePath
Gets the path and file name of the executable that started the application, the effect, and 11 samples.

2. Operating environment variables
System. Environment. GetEnvironmentVariable() method can be used to conveniently obtain system environment variables, such as:
System. Environment. GetEnvironmentVariable("windir") gives you the path to the windows system directory.

Here are 1 commonly used environment variable values:


At last, I posted the variable value obtained from the above operation. In advance, I wrote an WinForm program. The project file is stored in D:\Visual Studio \MyApplication\LifeAssistant, and the compiled file is located in D:\Visual Studio \MyApplication\ bin\Debug.

1, System. Diagnostics. Process. GetCurrentProcess () MainModule. FileName = D: \ Visual Studio Projects \ MyApplication \ LifeAssistant \ bin \ Debug \ LifeAssistant exe

System.Environment =D:\Visual Studio \MyApplication\ 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("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
System.Environment.GetEnvironmentVariable("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

Related articles: