[英]how to get publish version?
我也遇到了这个问题,发现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中指定的任何设置相匹配?
我们可以创建一个属性,它将返回如下所述的版本信息,我们可以使用该属性。
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);
}
}
}
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;
}
将C#6.0与Lambda表达式一起使用
private string GetVersion => ApplicationDeployment.IsNetworkDeployed ? $"Version: {ApplicationDeployment.CurrentDeployment.CurrentVersion}" : $"Version: {Application.ProductVersion}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.