我想显示我的桌面应用程序的发布版本。我试图用这段代码:如何获取发布版本?
_appVersion.Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
问题是我没有得到我的项目属性中的发布版本。下面是它的截图:
但我正在逐渐3.0.0.12546
。有人知道问题在哪里吗?
我想显示我的桌面应用程序的发布版本。我试图用这段代码:如何获取发布版本?
_appVersion.Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
问题是我没有得到我的项目属性中的发布版本。下面是它的截图:
但我正在逐渐3.0.0.12546
。有人知道问题在哪里吗?
我也有这个问题,发现AssemblyInfo.cs
设置的版本号是用一个集Properties
干扰:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
我通常评论的线条勾勒出的AssemblyInfo
和替换他们与
[assembly: AssemblyVersion("1.0.*")]
检查这些值是否是hard-co登入您的AssemblyInfo
档案。
有关自动版本控制的有趣讨论,请参阅this SO question。在检查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}";
你在检查调试模式? – Sajeetharan
Nop,在发布模式 – IrApp