如何在C#中获取当前的产品版本?

我如何以编程方式获取C#中的当前产品版本?

我的代码:

VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); 

我得到VersionNumber = 1.0.0.0,但是当前版本是1.0.0.12。

有三个版本:程序集,文件和产品。 要获得产品版本:

 using System.Reflection; using System.Diagnostics; Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); string version = fileVersionInfo.ProductVersion; 

我得到了我的问题的答案只是给System.Deployment.Application的参考,虽然它不会在Visual Studio的开发工作,但它将工作,一旦应用程序部署。

  //using System.Deployment.Application; //using System.Reflection; public string CurrentVersion { get { return ApplicationDeployment.IsNetworkDeployed ? ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() : Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } 
 System.Reflection.Assembly.GetEntryAssembly().GetName().Version 

获取产品版本(使用AssemblyInformationalVersionAttribute指定)的另一种方法是

 private static string AssemblyProductVersion { get { object[] attributes = Assembly.GetExecutingAssembly() .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false); return attributes.Length == 0 ? "" : ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion; } } 

所有这些答案都要求.GetExecutingAssembly()
如果你在dll中有这个代码,它会返回dll的版本号。

交换调用GetCallingAssembly()来获取你想要知道的代码。

 /// <summary> /// Returns version like 2.1.15 /// </summary> public static String ProductVersion { get { return new Version(FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).ProductVersion).ToString(); } } 

尝试这个:

 var thisApp = Assembly.GetExecutingAssembly(); AssemblyName name = new AssemblyName(thisApp.FullName); VersionNumber = "v. " + name.Version; 

另外,请参阅AssemblyName.Version属性上的此 MSDN文章。

在C#中,您需要使用reflection和诊断

 Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location); string version = fileVersionInfo.ProductVersion;