ASP. NET Summarizes 7 methods of obtaining current path in C

  • 2021-09-11 21:03:15
  • OfStack

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
-Gets the full path of the module.
2. System.Environment.CurrentDirectory
-Gets and sets the fully qualified directory of the current directory (the directory from which the process started).
3. System.IO.Directory.GetCurrentDirectory()
-Gets the current working directory of the application. This is the directory from which the program starts. It is possible that the program is placed in C:\ www, and this function may return C:\ Documents and Settings\ ZYB\, or C:\ Program Files\ Adobe\. Sometimes it is impossible to return something.
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 includes the application.
6. System.Windows.Forms.Application.StartupPath
-Gets the path to the executable file where the application was started. The effect was 2, 51 samples. It's just that there is an extra "\" after the string returned by 5
7. System.Windows.Forms.Application.ExecutablePath
-Get the path and filename of the executable file that started the application, effect and 11 samples.


// Gets the full path of the module. 
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// Gets and sets the current directory ( The directory from which the process started ) Fully qualified directory of 
string path2 = System.Environment.CurrentDirectory;
// Gets the current working directory of the application 
string path3 = System.IO.Directory.GetCurrentDirectory();
// Get the base directory of the program 
string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
// Gets and sets the name of the directory that includes the application 
string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
// Gets the path of the executable file that started the application 
string path6 = System.Windows.Forms.Application.StartupPath;
// Gets the path and file name of the executable file that started the application 
string path7 = System.Windows.Forms.Application.ExecutablePath;

StringBuilder str=new StringBuilder();
str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1);
str.AppendLine("System.Environment.CurrentDirectory:" + path2);
str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3);
str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4);
str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5);
str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6);
str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7);
string allPath = str.ToString();

/* Output results

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE
*/


Related articles: