어셈블리 이름 얻기


191

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"
}

답변:


350
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

또는

typeof(Program).Assembly.GetName().Name;

VS는 해결 사용시 오류를 표시합니다. Assembly.GetEntryAssembly (). GetName (). Name을 사용할 수 있습니다.
— Butsaty

3
실제로 그것은 typeof (any) .GetTypeInfo (). Assembly
— Thaina

7

어셈블리를 사용하여 양식 제목을 다음과 같이 설정합니다.

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;
}

1
그냥 당신이 전화하지 않는 기꺼이 그에서 오피스 추가 기능에서 - GetEntryAssembly ()가 null을 반환합니다
— PandaWood

3

System.Reflection.AssemblyTitleAttribute.Title속성 을 사용하는이 코드를 시도 할 수 있습니다.

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


2

AssemblyName어셈블리의 전체 이름이있는 경우 클래스를 사용하여 어셈블리 이름을 가져올 수 있습니다 .

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

또는

AssemblyName.GetAssemblyName(e.Source).Name

MSDN 참조-AssemblyName 클래스


2
GetAssemblyName 메서드의 매개 변수로 인해 오류가 발생했습니다. 나는 그것이 Assembly.GetExecutingAssembly().Location대신에 있어야한다고 생각합니다 Assembly.GetExecutingAssembly().FullName.
— uzay95

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.