【转载】C#获取项目程序路径的10种方法

转载自http://www.qimao.cn/article.asp?id=179

qimao.cn
奇猫居-神游天地间

1. 获取当前进程的完整路径,包含文件名(进程名)。

this.GetType().Assembly.Location;
返回格式: X:\xxx\xxx\xxx.exe

2. 获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
返回格式: X:\xxx\xxx\xxx.exe

3. 获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
System.Environment.CurrentDirectory;
返回格式: X:\xxx\xxx

4. 获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
System.AppDomain.CurrentDomain.BaseDirectory;
返回格式: X:\xxx\xxx\

5. 获取和设置包含该应用程序的目录的名称。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
返回格式: X:\xxx\xxx\ (.exe文件所在的目录+"\")

6. 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
System.Windows.Forms.Application.StartupPath;
返回格式: X:\xxx\xxx

7. 获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
System.Windows.Forms.Application.ExecutablePath;
返回格式: X:\xxx\xxx\xxx.exe

8. 获取应用程序的当前工作目录。(注:此方法取值不固定,随着OpenFileDialog、SaveFileDialog等对象所确定的目录而改变,解决办法,程序启动时System.IO.Directory.GetCurrentDirectory()保存到变量里,需要程序所在路径是,使用变量值。)
System.IO.Directory.GetCurrentDirectory();
返回格式: X:\xxx\xxx

9. WebForm使用:获取站点所在虚拟目录的物理路径
Request.PhysicalApplicationPath
返回格式: X:\xxx\xxx

10. 在卸载程序获取系统安装的目录:
System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();
string path=curPath.Location;//得到安装程序类SetupLibrary文件的路径,获取这个文件路径所在的目录即得到安装程序的目录
posted @ 2010-12-17 21:21  羽落无声  阅读(334)  评论(0编辑  收藏  举报