如何在我的日志中打印正在运行的程序的版本? 换句话说,我可以使用Console.WriteLine访问AssemblyFileVersion吗?
由于 贝
答案 0 :(得分:5)
看起来像这样的东西会起作用:
public static string Version
{
get
{
Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
}
}
答案 1 :(得分:3)
// Get the version of the current application.
Assembly assem = Assembly.GetExecutingAssembly();
AssemblyName assemName = assem.GetName();
Version ver = assemName.Version;
Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
有关MSDN的更多信息:
答案 2 :(得分:0)
FileVersionInfo.GetVersionInfo(asm.Location)不适用于嵌入式程序集(例如,使用Fody's Costura发送单个EXE而不是可执行文件及其所有依赖程序集。在这种情况下,以下作为后备工作:
var assembly = Assembly.GetExecutingAssembly();
var fileVersionAttribute = assembly.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == typeof(AssemblyFileVersionAttribute));
if (fileVersionAttribute != null && fileVersionAttribute.ConstructorArguments.Any())
return fileVersionAttribute.ConstructorArguments[0].ToString().Replace("\"","");
return string.Empty;