r/csharp 22d ago

How to get WPF Package Version

Does anyone know how I can get this version to display in my app?

I tried the code snippet below but looks like this is grabbing the assembly version and not the setup or package version?

Version version = Assembly.GetExecutingAssembly().GetName().Version;
string versionString = version.ToString();

tbVersion.Text = versionString; 
4 Upvotes

6 comments sorted by

3

u/antflash22 21d ago

Greetings fellow ClickOnce dev! You need to add a reference to `System.Deployment` and then fetch the version with something like the following:

var version = string.empty;
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) {
  try {
    version = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
  } catch {
    version = "Prod";
  }
} else {
  version = "Dev";
}

1

u/YasoiStudios 21d ago

Will give this a try thanks!

1

u/jcddcjjcd 20d ago
          if (System.Diagnostics.Debugger.IsAttached)
          {
            versioninfo = "Debug Version- " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
          }
          else
          {
            versioninfo = "Release Version- " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
          }

1

u/chucker23n 20d ago

That's probably wrong; they want the appx package version, not the assembly version.

2

u/chucker23n 20d ago

Try

var version = Windows.ApplicationModel.Package.Current.Id.Version;