Reputation: 75
I am developing a WPF application in .NET 6. The application will be published using ClickOnce and my goal is to display the number of the version in the application. I saw System.Deployment, but unfortunately this seems to be available only for .NET Framework applications. My first thought on this problem was to use the standard Publish.html file and read the version from it. However, this solution feels a little bit weird and does only work partially, as it displays the most current version and not the version the user has actually installed.
Are there any other approaches for this issue?
Upvotes: 4
Views: 3515
Reputation: 51
Since .NET 7, we can get ClickOnce's version. I succeeded it.
Environment.GetEnvironmentVariable("ClickOnce_CurrentVersion")
Upvotes: 5
Reputation: 51
I was having the same issue, and in my case the published version was really the only version I cared to display. My solution was to overwrite the assembly version with the published version by adding the following target to my .csproj file:
<Target Name="SetAssemblyVersion" BeforeTargets="BeforeCompile">
<FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
<Output PropertyName="AssemblyVersion" TaskParameter="OutputVersion" />
</FormatVersion>
<FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
<Output PropertyName="FileVersion" TaskParameter="OutputVersion" />
</FormatVersion>
</Target>
After doing this, the version given by Assembly.GetExecutingAssembly().GetName().Version
in your published application will be the same as your published version.
Note that this overwrites the actual assembly version, and will put something like 1.0.0.0 if you build without publishing. For my purposes this didn't matter, but I'm sure you can add some sort of condition which prevents this target from running outside of publish builds.
Upvotes: 5