C# 获取当前应用程序集路径

当前应用程序路径

一个WinForm程序,项目文件存放于D:ProjectsDemo,编译后的文件位于
D:ProjectsDemoinDebug,最后的结果如下:

代码 结果
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName D:ProjectsDemoinDebugDemo.exe
System.Environment.CurrentDirectory D:ProjectsDemoinDebug
System.IO.Directory.GetCurrentDirectory() D:ProjectsDemoinDebug
System.AppDomain.CurrentDomain.BaseDirectory D:ProjectsDemoinDebug
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase D:ProjectsDemoinDebug
System.Windows.Forms.Application.StartupPath D:ProjectsDemoinDebug
System.Windows.Forms.Application.ExecutablePath D:ProjectsDemoinDebugDemo.EXE

这里需要注意,在一次偶然的测试中,通过添加注册表启动记录实现了开机运行该程序,结果路径输出有变化

System.Environment.CurrentDirectorySystem.IO.Directory.GetCurrentDirectory() 的返回值变成了 C:Windowssystem32

PS: 添加注册表记录实现开机自启的方法

Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "随便什么名字", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

当前系统环境路径

代码 结果
System.Environment.GetEnvironmentVariable("windir") C:WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE") C:Program FilesMicrosoft Visual Studio.NET 2005SDKv2.0include
System.Environment.GetEnvironmentVariable("TMP") C:DOCUME1ADMINI1LOCALS~1Temp
System.Environment.GetEnvironmentVariable("TEMP") C:DOCUME1ADMINI1LOCALS~1Temp
System.Environment.GetEnvironmentVariable("Path") C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Program FilesMicrosoft SQL Server90Toolsinn
原文地址:https://www.cnblogs.com/cplemom/p/12451597.html