如何获取 WPF 应用程序的发布版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22527830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the publish version of a WPF application
提问by Raj123
I want my WPF application publish version. I tried using the answer for thisquestion. It works but the problem is we can manually change the values there. I want to know how many times my project was actually published (don't need version number. Just how many times did I publish my application). Can this be done?
我想要我的 WPF 应用程序发布版本。我尝试使用这个问题的答案。它有效,但问题是我们可以手动更改那里的值。我想知道我的项目实际发布了多少次(不需要版本号。我发布了多少次应用程序)。这能做到吗?
回答by Sheridan
Using Click Once, each time you publish, Visual Studio will change the number automatically. It will increment the value each time you publish. Your problem is that you have manually changed the number. The solution is to publish and just let Visual Studio update the value... you should notice that your project needs to be saved once you have published. This is because Visual Studio just incremented the value for you.
使用 Click Once,每次发布时,Visual Studio 都会自动更改数字。每次发布时它都会增加该值。您的问题是您手动更改了号码。解决方案是发布并让 Visual Studio 更新值……您应该注意到,一旦发布,您的项目就需要保存。这是因为 Visual Studio 只是为您增加了值。
UPDATE >>>
更新 >>>
If you want to access the published version from code (which you shouldhave made clear in your question), then you can use this code, but you have to ensure that the application is network deployed first... that means that it has actually been published, so it won't work while you are debugging. Try this:
如果您想从代码中访问已发布的版本(您应该在问题中明确说明),那么您可以使用此代码,但您必须确保该应用程序首先是网络部署的......这意味着它实际上已经已发布,因此在您调试时它不起作用。尝试这个:
private string GetPublishedVersion()
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.
CurrentVersion.ToString();
}
return "Not network deployed";
}
回答by Mike Keskinov
You may be confused by 2 sets of numbers. Please note that you can set version of your WPF app in TWO different places:
您可能会对 2 组数字感到困惑。请注意,您可以在两个不同的地方设置 WPF 应用程序的版本:
- Project Properties / Publish tab / Publish Version
AssemblyVersiondeclared in AssemblyInfo.csfile, which you can find if you expand Project Properties node in Solution Explorer.
- 项目属性/发布选项卡/发布版本
AssemblyVersion在AssemblyInfo.cs文件中声明,如果在解决方案资源管理器中展开项目属性节点,您可以找到该文件。
They are similar in the sense that they both provides 4 numbers: major, minor, build and revision. The difference is that Publish Versionis only availableif the app was actually Published (i.e. Installed). It is not available in your debug session nor if you just copy the executable to another machine and run it there. SO, if you just need to track version of your EXE file, use AssemblyInfo.cs.
它们的相似之处在于它们都提供了 4 个数字:主要、次要、构建和修订。不同的是,Publish Version是唯一可用的,如果应用程序实际上已发布(即安装)。它在您的调试会话中不可用,也不能将可执行文件复制到另一台机器并在那里运行。所以,如果您只需要跟踪 EXE 文件的版本,请使用AssemblyInfo.cs。
Correspondingly, to read the data use the following code:
相应地,要读取数据,请使用以下代码:
1 To read Publish version(declared in Publish tab)
1 阅读发布版本(在发布选项卡中声明)
using System.Deployment.Application;
ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
Note that in this case: a) you need to add reference to System.Deploymentassembly, b) if the app was not deployed, it won't work.
请注意,在这种情况下:a) 您需要添加对System.Deployment程序集的引用,b) 如果未部署应用程序,它将无法工作。
2 To read Assembly Version(declared in AssemblyInfo.cs)
2 读取Assembly Version(在AssemblyInfo.cs 中声明)
Assembly.GetExecutingAssembly().GetName().Version;
This one always works.
这个总是有效的。
回答by Danil Shaykhutdinov
Universal solution if we get application version from not startup assembly:
如果我们从非启动程序集中获取应用程序版本,则通用解决方案:
var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
string appVersion = $"{version.Major}.{version.Minor}";
GetEntryAssembly give version of startup project.
GetEntryAssembly 给出启动项目的版本。
回答by MegaMind
var obj=Assembly.GetExecutingAssembly().GetName().Version;
string version= string.Format("Application Version {0}.{1}", obj.Build, obj.Revision);
OR
string version= string.Format("Application Version {0}.{1}", obj.Major, obj.Minor);
whichever properties suits you.
哪个属性适合你。

