一、获取当前文件的路径
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
7. System.Windows.Forms.Application.ExecutablePath
获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
二、操作环境变量
利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系统环境变量,如:
System.Environment.GetEnvironmentVariable("windir")就可以取得windows系统目录的路径。
以下是一些常用的环境变量取值:
System.Environment.GetEnvironmentVariable("windir");
System.Environment.GetEnvironmentVariable("INCLUDE");
System.Environment.GetEnvironmentVariable("TMP");
System.Environment.GetEnvironmentVariable("TEMP");
System.Environment.GetEnvironmentVariable("Path");
最后贴出我进行上面操作获得的变量值,事先说明,本人是编写了一个WinForm程序,项目文件存放于D:/Visual Studio Projects/MyApplication/LifeAssistant,编译后的文件位于D:/Visual Studio Projects/MyApplication/LifeAssistant/bin/Debug,最后的结果如下:
1、 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:/Visual Studio Projects/MyApplication/LifeAssistant/bin/Debug/LifeAssistant.exe
2、System.Environment.CurrentDirectory=D:/Visual Studio Projects/MyApplication/LifeAssistant/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("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
转载:http://blog.sina.com.cn/s/blog_5d3d54340100t4io.html
|
请发表评论