11

I am looking to use code similar to the below, but in a .NET Core WPF C# application. This is what I previously used.

public static string GetVersion()
{
   if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
   {
      Version myVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion;
      return $"Version {myVersion}";
   }
   return "Version not deployed";
}

Is there an equivalent in .NET Core?

Hugh W
  • 716
  • 2
  • 10
  • 33
scottsanpedro
  • 1,142
  • 2
  • 12
  • 28

3 Answers3

6

There is a closed issue around this issue and ClickOnce deployment.

ApplicationDeployment class is not available - that class is implemented in System.Deployment assembly that is not part of .NET Core 3.1 or .NET 5.

We will have a way to expose some of the properties that the class enables, i.e. URL parameters and update status. But, that work could not be completed for .NET 5 release. We will enable it in one of .NET 6 previews. Here are the tracking issues: #27 and #53

Use of ApplicationDeployment class in your deployed ClickOnce application is not possible, but main ClickOnce experience is available. It includes: creating ClickOnce deployment (manifests), publishing the application, creating and publishing updates. Your application will get automatic update check (and update, if user accepts it) - this part is handled by ClickOnce runtime and the new Launcher tool that becomes the entry point of ClickOnce deployment for .NET Core 3.1 and .NET 5 applications.

Consequently, as for now there does not seem to be a way to do this in .NET Core directly.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • 1
    And how can it be done indirectly? – Istvan Heckl Oct 29 '21 at 13:28
  • @IstvanHeckl What I mean is that this is not built into the framework. – thatguy Oct 29 '21 at 13:41
  • 2
    Ok, I get it is not in the framework. But what is the alternative? I still want to display the CurrentVersion, I also want to display the CurrentDeployment.DataDirectory (so the users now where is the mdf file). To solve the first problem I could hard code a string and change it when there is a new version (not nice). For the second problem I have not idea. – Istvan Heckl Oct 30 '21 at 05:56
4

There has been some progress on this in .NET. According to a comment by a Microsoft employee you can now detect these values via environment variables as is done in mage.exe. For example something like this:

bool.TryParse(Environment.GetEnvironmentVariable("ClickOnce_IsNetworkDeployed"), out bool isNetworkDeployed);
Version.TryParse(Environment.GetEnvironmentVariable("ClickOnce_CurrentVersion"), out Version currentVersion);

Most of the discussion on that GitHub issue is about .NET 7. On the other hand, some comments suggest this code has its own independent release schedule. I don't know enough to say if/when it will be available in Core.

Hugh W
  • 716
  • 2
  • 10
  • 33
  • 1
    Nice idea, but check if it works in your custom case! . . . . . For me it does not work and always `False` is returned. (.NET Core 3.1, created ClickOnce setup with `vsbuild` (msbuild) inside of an Azure build pipeline instead of `mage.exe`) – Beauty Aug 09 '23 at 17:09
2

You should be able to get the assembly version using System.Reflection.Assembly.GetExecutingAssembly().GetName().Version, as shown in Reading Assembly version information of WPF application

For more click-once capabilities not ported to .net core3.1/5/6, look here https://github.com/derskythe/WpfSettings

dbonstack
  • 49
  • 4
  • 1
    But that's the assembly version (`` in your SDK-style .csproj file). This is not necessarily the same as the ClickOnce publish version (`` in your .pubxml file). – Hugh W Jan 14 '23 at 08:14