C# 如何在文本框中显示发布版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17690378/
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 show publish version in a textbox?
提问by Shane Superfly MacNeill
At the moment I am manually updating the version field (textbox
) in my application every time I publish it. I am wondering if there is a way to have my application get that data from somewhere and display it in the box for me. I am using VS2012 and I am just unsure of how to achieve that in C#. Below is a screenshot of the VS2012 properties window that I am talking about.
目前textbox
,我每次发布时都会手动更新应用程序中的版本字段 ( )。我想知道是否有办法让我的应用程序从某个地方获取该数据并将其显示在框中。我正在使用 VS2012,我只是不确定如何在 C# 中实现它。下面是我正在谈论的 VS2012 属性窗口的屏幕截图。
NEW IMAGE:
新图片:
采纳答案by Ralph Jansen
Don't forget to check if the application is networkdeployed otherwise it won't work in debug mode.
不要忘记检查应用程序是否网络部署,否则它将无法在调试模式下工作。
if (ApplicationDeployment.IsNetworkDeployed)
{
this.Text = string.Format("Your application name - v{0}",
ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4));
}
回答by Karl Anderson
Try this:
尝试这个:
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
Then the caller to the getter property can de-reference the Major
, Minor
, Build
and Revision
properties, like this:
然后主叫方,吸气属性可以去参考Major
,Minor
,Build
和Revision
属性,就像这样:
YourVersionTextBox.Text = AssemblyVersion.Major.ToString() + "."
+ AssemblyVersion.Minor.ToString() + "."
+ AssemblyVersion.Build.ToString() + "."
+ AssemblyVersion.Revision.ToString();
回答by Shiju Madamchery
Also we can use overloadedToString
of System.Version
我们也可以使用重载ToString
的System.Version
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
YourVersionTextBox.Text = AssemblyVersion.ToString(1); // 1 - get only major
YourVersionTextBox.Text = AssemblyVersion.ToString(2); // 1.0 - get only major, minor
YourVersionTextBox.Text = AssemblyVersion.ToString(3); // 1.0.3 - get only major, minor, build
YourVersionTextBox.Text = AssemblyVersion.ToString(4); // 1.0.3.4 - get only major, minor, build, revision
回答by Christos Lytras
If you get an error of ApplicationDeployment
, then you can try this:
如果您收到 错误ApplicationDeployment
,那么您可以尝试以下操作:
For global version accessing through Program
, declare inside Program
class:
对于通过 访问的全局版本Program
,在Program
类内部声明:
private static Version version = new Version(Application.ProductVersion);
public static Version Version
{
get
{
return version;
}
}
Now you have Program.Version
anywhere available in the program and you can use it to get version information like this:
现在您Program.Version
在程序中的任何地方都可以使用,您可以使用它来获取版本信息,如下所示:
LabelVersion.Text = String.Format("Version {0}.{1}",
Program.Version.Major.ToString(),
Program.Version.Minor.ToString());
回答by Arash.Zandi
Method 1 :You can use this
方法一:你可以用这个
string version = Application.ProductVersion;
and show the versionin your textBox.
并在您的文本框中显示版本。
Method 2 :or if you want the version parts separately, you can use this :
方法 2 :或者如果你想要单独的版本部分,你可以使用这个:
System.Version version2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
now you have these :
现在你有这些:
version2.Major;
version2.Minor;
version2.Revision;
version2.Build;
and you can use them like this
你可以像这样使用它们
string versionString= (String.Format("{0}.{1}.{2}.{3}", version2.Major, version2.Minor, version2.Revision, version2.Build));