HOWTO obtain the ClickOnce Version at runtime
7 May 2007This article will show you how to get the ClickOnce Version at runtime of you .NET application. My examples are in VB.NET and C# but the same technique would work for any other .NET Assembly.
First you need to add a project reference to System.Deployment
Next import the namespace into your class
VB.NET
Imports System.Deployment
C#
using System.Deployment;
You can obtain the Current Version from the ApplicationDeployment.CurrentDeployment.CurrentVersion property. This returns a System.Version object.
Note (from MSDN): CurrentVersion will differ from UpdatedVersion if a new update has been installed but you have not yet called Restart. If the deployment manifest is configured to perform automatic updates, you can compare these two values to determine if you should restart the application.
Retrieve the ClickOnce Version from the CurrentVersion property
NOTE: The CurrentDeployment static property is only valid when the application has been deployed with ClickOnce. Therefore before you access this property, you should check the ApplicationDeployment.IsNetworkDeployed property first, it will always return a false in the debug environment.
VB.NET
Dim myVersion as Version if (ApplicationDeployment.IsNetworkDeployed) then myVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion end if
C#
Version myVersion; if (ApplicationDeployment.IsNetworkDeployed) myVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion;
Using the Version object
From here you can use the version information in a label, say on an about form in this way.
VB.NET
label1.Text = string.Format("ClickOnce published Version: v{0}.{1}.{2}.{3}", myVersion.Major, myVersion.Minor, myVersion.Build, myVersion.Revision)
C#
label1.Text = string.Format("ClickOnce published Version: v{0}.{1}.{2}.{3}", myVersion.Major, myVersion.Minor, myVersion.Build, myVersion.Revision);
No comments yet
Leave a Reply
You must be logged in to post a comment.
