zoukankan      html  css  js  c++  java
  • 转:C# WinForm获取 当前执行程序路径的几种方法

    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 

  • 相关阅读:
    crs_stop 错误一列
    本地管理表空间和字典管理表空间的特点,ASSM有什么特点
    RAC 11.2的新特性
    oracle 11g RAC 的一些基本概念(三)
    【1】【leetcode-130】 被围绕的区域
    【leetcode-125】 验证回文串
    【1】【leetcode-127】单词接龙word-ladder
    【1】【leetcode-115】 不同的子序列 distinct-subsequences
    【1】[leetcode-124] 二叉树中的最大路径和
    [leetcode-128] 最长连续序列
  • 原文地址:https://www.cnblogs.com/yan212/p/6187942.html
Copyright © 2011-2022 走看看