c#如何打印AssemblyFileVersion

时间:2010-08-03 21:38:51

标签: c#

如何在我的日志中打印正在运行的程序的版本? 换句话说,我可以使用Console.WriteLine访问AssemblyFileVersion吗?

由于 贝

3 个答案:

答案 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);
    }
}

来自another post on SO

答案 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的更多信息:

Version Class

答案 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;