代码之家  ›  专栏  ›  技术社区  ›  Martin Hollingsworth

Silverlight 3替代fileVersionInfo.GetVersionInfo

  •  5
  • Martin Hollingsworth  · 技术社区  · 16 年前

    在Silverlight 3.0应用程序中,我希望使用 AssemblyFileVersion 显示应用程序的版本信息。这和 AssemblyVersion 通常在.NET应用程序中使用以下代码检索和:

    var executingAssembly = Assembly.GetExecutingAssembly();
    var fileVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly.Location);
    var versionLabel = fileVersionInfo.FileVersion;
    

    很遗憾,Silverlight 3.0运行时不包括 FileVersionInfo 班级。是否有其他方法访问此信息?

    2 回复  |  直到 16 年前
        1
  •  5
  •   Sam Harwell    16 年前

    这里有一种使用属性的方法-我不确定它是否可以在Silverlight中工作,所以您必须通知我。

    Assembly assembly = Assembly.GetExecutingAssembly();
    object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
    if (attributes != null && attributes.Length > 0)
    {
        AssemblyFileVersionAttribute fileVersionAttribute = (AssemblyFileVersionAttribute)attributes[0];
        string version = fileVersionAttribute.Version;
    }
    
        2
  •  3
  •   Martin Hollingsworth    16 年前

    我在Twitter上找到了一个解决方案 Craig Young (由谷歌网页缓存提供)使用 Assembly.GetCustomAttributes 如下

    var executingAssembly = Assembly.GetExecutingAssembly();
    var customAttributes = executingAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
    if (customAttributes != null)
    {
       var assemblyFileVersionAttribute = customAttributes[0] as AssemblyFileVersionAttribute;
       var fileVersionLabel = assemblyFileVersionAttribute.Version;
    }
    

    发布此解决方案以备将来参考。