C # file path operations summary

First, get the path of the current file

1. System.Diagnostics.Process.GetCurrentProcess (). MainModule.FileName
     get the full path to the module, including the file name.
2. System.Environment.CurrentDirectory
     get and set the current directory (the directory from which you started the process) fully qualified directory.
3. System.IO.Directory.GetCurrentDirectory ()
     Gets the current working directory of the application. This is not necessarily the start of the program from the directory ah, it is possible to program in C: / www where this function it is possible to return C: / Documents and Settings / ZYB /, or C: / Program Files / Adobe / , sometimes not necessarily return what stuff, this is the last time any application operating over the directory, for example, you use Word to open the E: /doc/my.doc this file, then the implementation of this method returns the E: / doc up.
4. System.AppDomain.CurrentDomain.BaseDirectory
     acquiring program directory group.
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
     Get and set the directory name includes the application program.
6. System.Windows.Forms.Application.StartupPath
     obtain the path for the executable file of the application. And the same effect 2,5. Just behind the 5 string returned more than a "/" only
7. System.Windows.Forms.Application.ExecutablePath
     get started the application's executable file path and file name, the same effect and 1. 

Second, the operating environment variables

Use System.Environment.GetEnvironmentVariable () method can easily obtain the system environment variables, such as:
System.Environment.GetEnvironmentVariable ( "windir") you can get windows system directory path.
Here are some common environment variables values:
System.Environment.GetEnvironmentVariable ( "windir");
System.Environment.GetEnvironmentVariable ( "the INCLUDE");
System.Environment.GetEnvironmentVariable ( "TMP");
System.Environment.GetEnvironmentVariable ( " the TEMP ");
System.Environment.GetEnvironmentVariable (" Path ");

Finally, I put the variable values were obtained by the above operation, the prior statement, I am writing a WinForm program, project files are stored on D: / Visual Studio Projects / MyApplication / LifeAssistant, the compiled file is located in D: / Visual Studio Projects / MyApplication / LifeAssistant / bin / Debug, the final results are as follows:
. 1, System.Diagnostics.Process.GetCurrentProcess () = D MainModule.FileName:. / the Visual Studio the Projects / the MyApplication / LifeAssistant / bin / the Debug / LifeAssistant.exe
2, the System D = .Environment.CurrentDirectory: / the Visual Studio the Projects / the MyApplication / LifeAssistant / bin / the Debug
. 3, System.IO.Directory.GetCurrentDirectory () = D: / the Visual Studio the Projects / the MyApplication / LifeAssistant / bin / the 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:/DOCUME~1/zhoufoxcn/LOCALS~1/Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:/DOCUME~1/zhoufoxcn/LOCALS~1/Temp
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

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/11/04/2671032.html

Guess you like

Origin blog.csdn.net/weixin_34378969/article/details/93053089