views:

455

answers:

2

In my C# ClickOnce app, there is an auto-incremented Publish Version in the Project->Properties->Publish tab. I'd like to display that version in my Help->About box, but the code I'm using apparently accesses the Assembly Version, which is different. The Assembly Version can be changed manually in the Project->Propteries->Application->Assembly Information dialog. So for now, every time before I publish I've been copying the Publish Version to the Assembly version, so my dialog shows the current version of the app. There must be a better way to do this.

All I really want to do is have an accurate, auto-updated, code-accessible version number.

Here's the code I'm using to access the Assembly Version number:

public string AssemblyVersion
{
    get
    {
        return Assembly.GetExecutingAssembly().GetName().Version.ToString();
    }
}

An alternative would be to find code that accesses the Publish Version.

Thanks in advance,
Randy

+4  A: 

I modified my .csproj file to update the assembly version. I created a configuration called "Public Release" for this - but its not required to do that

  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <MSBuildCommunityTasksPath>$(SolutionDir)Tools\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
  </PropertyGroup>
  <!-- Required Import to use MSBuild Community Tasks -->
  <Import Project="$(SolutionDir)Tools\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" Condition="'$(BuildingInsideVisualStudio)' == 'true'" />
  <Target Name="BeforeCompile" Condition="'$(BuildingInsideVisualStudio)|$(Configuration)' == 'true|PublicRelease'">  
    <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
      <Output TaskParameter="OutputVersion" PropertyName="AssemblyVersionToUse" />
    </FormatVersion>
    <AssemblyInfo CodeLanguage="CS" OutputFile="$(ProjectDir)Properties\VersionInfo.cs" AssemblyVersion="$(AssemblyVersionToUse)" AssemblyFileVersion="$(AssemblyVersionToUse)" />
  </Target>

The published version may be:

ApplicationDeployment.CurrentDeployment.CurrentVersion
sylvanaar
+1  A: 

sylvanaar's last line looks like the way to go, in my experience; but with the caveat that it is only available to deployed versions of the application. For debugging purposes, you might want something like:

    static internal string GetVersion()
    {
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            return ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
        }

        return "Debug";
    }
Richard Dunlap
Thanks much - this is the simplest way to go, and it seems to work like a charm!
Randy Gamage
It's not ideal though, because when you right-click an assembly and view properties, the version displayed is still the AssemblyVersion.
romkyns