public string VersionLabel
{
get
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
}
else
{
var ver = Assembly.GetExecutingAssembly().GetName().Version;
return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
}
}
}
我之前也遇到过这个问题,发现在AssemblyInfo.cs
中设置的版本号与Properties
中设置的版本号产生了冲突。
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
我通常会将那些AssemblyInfo
中的行注释掉并替换为
[assembly: AssemblyVersion("1.0.*")]
请检查这些值是否已经硬编码到您的 AssemblyInfo
文件中。
参见这个SO问题,了解有关自动版本控制的有趣讨论。在检查 AssemblyInfo.cs
时,请确保您的自动递增(如果使用)只针对 AssemblyVersion
而不是 AssemblyFileVersion
。
调试程序时,您可以检查程序集的属性。
\bin\Release\app.publish
在 Details
选项卡下,检查版本号。它是否与您在 VS 中指定的任何设置匹配?
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
使用这个命令将会获取到存在于 AssemblyInfo.cs 文件中的程序集版本,如果想要获取在发布对话框中设置的发布版本,则应该使用
System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion
请注意,您需要添加对System.Deployment的引用,而且它仅在通过右键单击项目文件并单击发布发布应用程序后才能工作,每次发布都会增加修订版本。
如果您尝试在调试模式下调用上述行,则不起作用并将引发异常,因此您可以使用以下代码:
try
{
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
catch(Exception ex)
{
return Assembly.GetExecutingAssembly().GetName().Version;
}
private string GetVersion => ApplicationDeployment.IsNetworkDeployed
? $"Version: {ApplicationDeployment.CurrentDeployment.CurrentVersion}"
: $"Version: {Application.ProductVersion}";