代码之家  ›  专栏  ›  技术社区  ›  Patrick

获取程序集名称

  •  166
  • Patrick  · 技术社区  · 14 年前

    C的异常类有一个源属性,该属性默认设置为程序集的名称。
    有没有其他方法可以得到这个确切的字符串(不需要解析其他字符串)?

    我试过以下方法:

    catch(Exception e)
    {
        string str = e.Source;         
        //"EPA" - what I want               
        str = System.Reflection.Assembly.GetExecutingAssembly().FullName;
        //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        str = typeof(Program).FullName;
        //"EPA.Program"
        str = typeof(Program).Assembly.FullName;
        //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        str = typeof(Program).Assembly.ToString();
        //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        str = typeof(Program).AssemblyQualifiedName;
        //"EPA.Program, EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    }
    
    5 回复  |  直到 10 年前
        1
  •  325
  •   icecrime    14 年前
    System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
    

    typeof(Program).Assembly.GetName().Name;
    
        2
  •  6
  •   Shimmy Weitzhandler 500 - Internal Server Error    10 年前

    我使用程序集设置窗体的标题:

    private String BuildFormTitle()
    {
        String AppName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
        String FormTitle = String.Format("{0} {1} ({2})", 
                                         AppName, 
                                         Application.ProductName, 
                                         Application.ProductVersion);
        return FormTitle;
    }
    
        3
  •  3
  •   user6438653user6438653    8 年前

    您可以尝试使用 System.Reflection.AssemblyTitleAttribute.Title 属性:

    ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute), false)).Title;

        4
  •  2
  •   John Smith jjcaicedo    8 年前

    你可以使用 AssemblyName 类获取程序集名称,前提是您具有程序集的全名:

    AssemblyName.GetAssemblyName(Assembly.GetExecutingAssembly().FullName).Name
    

    AssemblyName.GetAssemblyName(e.Source).Name
    

    MSDN Reference - AssemblyName Class

        5
  •  0
  •   ivan.ukr    7 年前

    Assembly.GetExecutingAssembly().位置