Pregunta

C # 's clase de excepción tiene una propiedad de origen que se establece en el nombre de la asamblea por defecto.
¿Hay alguna otra forma de obtener esta cadena exacta (sin necesidad de analizar una cadena diferente)?

He intentado lo siguiente:

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"
}
¿Fue útil?

Solución

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

o

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

Otros consejos

Yo uso la Asamblea para establecer el título de la forma como tal:

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

Se puede probar este código que utiliza la propiedad System.Reflection.AssemblyTitleAttribute.Title:

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

Puede utilizar la clase AssemblyName para obtener el nombre de ensamblado, siempre que tenga el nombre completo para el montaje:

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

o

AssemblyName.GetAssemblyName(e.Source).Name

MSDN Referencia - AssemblyName clase

Assembly.GetExecutingAssembly (). Localización

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top