Silverlight 3替代FileVersionInfo.GetVersionInfo

Mar*_*rth 5 .net silverlight silverlight-3.0

在Silverlight 3.0应用程序中,我想使用AssemblyFileVersion来显示应用程序的版本信息.这与AssemblyVersion不同,通常使用以下代码在.NET应用程序中检索:

var executingAssembly = Assembly.GetExecutingAssembly();
var fileVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly.Location);
var versionLabel = fileVersionInfo.FileVersion;
Run Code Online (Sandbox Code Playgroud)

不幸的是,Silverlight 3.0运行时不包含FileVersionInfo类.有没有其他方法来访问此信息?

Sam*_*ell 5

这是一种使用属性的方法 - 我不确定它是否适用于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;
}
Run Code Online (Sandbox Code Playgroud)

  • @Martin,也许你可以在问题中发布一些相关内容,这样人们就不会浪费时间去寻求帮助. (2认同)

Mar*_*rth 4

我在Craig Young的 twitter 帖子(由 Google 的页面缓存提供)中使用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;
}
Run Code Online (Sandbox Code Playgroud)

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