C# AssemblyFileVersion de uso dentro de un programa de
-
22-08-2019 - |
Pregunta
Estoy trabajando en un programa, y estoy tratando de mostrar la asamblea ARCHIVO versión
public static string Version
{
get
{
Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
return String.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart);
}
}
Por el momento, esto sólo devuelve los dos primeros números de versión en el "AssemblyVersion", no "AssemblyFileVersion." Realmente me gustaría simplemente hacer referencia a la AssemblyFileVersion en lugar de almacenar una variable interna llamada "Versión" que tengo que actualizar esto y la asamblea de la versión...
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("3.5.0")]
Esa es mi AssemblyFileVersion de AssemblyInfo.cs.Me gustaría hacer referencia a la "3.5.x", no el "1.0.*" :/
Gracias, Zack
Solución
Uso ProductMajorPart / ProductMinorPart en lugar de FileMajorPart / FileMinorPart:
public static string Version
{
get
{
Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
}
}
Otros consejos
using System.Reflection;
using System.IO;
FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
Console.WriteLine("AssemblyVersion : {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());
Console.WriteLine ("AssemblyFileVersion : {0}" , fv.FileVersion.ToString ());
var fileVersion = GetCustomAttributeValue<AssemblyFileVersionAttribute>(assembly, "Version");
private static string GetCustomAttributeValue<T>(Assembly assembly, string propertyName)
where T : Attribute
{
if (assembly == null || string.IsNullOrEmpty(propertyName)) return string.Empty;
object[] attributes = assembly.GetCustomAttributes(typeof(T), false);
if (attributes.Length == 0) return string.Empty;
var attribute = attributes[0] as T;
if (attribute == null) return string.Empty;
var propertyInfo = attribute.GetType().GetProperty(propertyName);
if (propertyInfo == null) return string.Empty;
var value = propertyInfo.GetValue(attribute, null);
return value.ToString();
}
Para obtener la versión del montaje Actualmente se puede utilizar la ejecución:
using System.Reflection;
Version version = Assembly.GetExecutingAssembly().GetName().Version;
La clase Asamblea también puede cargar archivos y acceder a todos los ensamblados cargados en un proceso.
supongo que tendrá que utilizar la clase FileVersionInfo.
System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)
protected void Application_Start(object sender, EventArgs e)
{
_log.InfoFormat("*************{0} **** Version: {1}************ ", Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version);
}
salida
Información Global - ************* CustomerFile **** Versión: 1.0.17.2510 ************