现在的位置: 首页 > 综合 > 正文

c# winform 获取当前程序运行根目录

2013年09月21日 ⁄ 综合 ⁄ 共 1132字 ⁄ 字号 评论关闭

1.获取和设置当前目录的完全限定路径。

string str = System.Environment.CurrentDirectory;

Result: C:xxxxxx

2.获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str = System.Windows.Forms.Application.StartupPath;

Result: C:xxxxxx

3.获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名。

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

Result: C:xxxxxxxxx.exe

4.获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。

string str = System.AppDomain.CurrentDomain.BaseDirectory;

Result: C:xxxxxx

5.获取应用程序的当前工作目录。

string str = System.IO.Directory.GetCurrentDirectory();

Result: C:xxxxxx

6.获取和设置包含该应用程序的目录的名称。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

Result: C:xxxxxx

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

string str = this.GetType().Assembly.Location;

Result: C:xxxxxxxxx.exe

8.获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string str = System.Windows.Forms.Application.ExecutablePath;

Result: C:xxxxxxxxx.exe


---------------------------------------------------------------------------------

在dll中有时需要使用主调用程序中的资源,这就要正确获取调用程序的文件名及其路径等信息。这需要和调用dll本身的文件名和路径区分开来!

 

    这就牵扯到System.Reflection.Assembly程序集类使用了。

     GetExecutingAssembly : 获取包含当前执行的代码的程序集

     GetCallingAssembly : 返回调用当前正在执行的方法的方法的
System.Reflection.Assembly 

抱歉!评论已关闭.