ASP.NET总结C#中7种获取当前路径的方法
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
-获取模块的完整路径。
2. System.Environment.CurrentDirectory
-获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
3. System.IO.Directory.GetCurrentDirectory()
-获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。
4. System.AppDomain.CurrentDomain.BaseDirectory
-获取程序的基目录。
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
-获取和设置包括该应用程序的目录的名称。
6. System.Windows.Forms.Application.StartupPath
-获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
7. System.Windows.Forms.Application.ExecutablePath
-获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
//获取模块的完整路径。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //获取和设置当前目录(该进程从中启动的目录)的完全限定目录 string path2 = System.Environment.CurrentDirectory; //获取应用程序的当前工作目录 string path3 = System.IO.Directory.GetCurrentDirectory(); //获取程序的基目录 string path4 = System.AppDomain.CurrentDomain.BaseDirectory; //获取和设置包括该应用程序的目录的名称 string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取启动了应用程序的可执行文件的路径 string path6 = System.Windows.Forms.Application.StartupPath; //获取启动了应用程序的可执行文件的路径及文件名 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();
/* 输出结果
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
*/
您可能感兴趣的文章:
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#比较二个数组并找出相同或不同元素的方法