如何在.NET ClickOnce应用程序中将发布版本与程序集版本同步?

问题描述 投票:31回答:5

在我的C#ClickOnce应用程序中,项目 - >属性 - >发布选项卡中有一个自动递增的发布版本。我想在我的菜单帮助 - >关于框中显示该版本,但我正在使用的代码显然访问了程序集版本,这是不同的。

可以在项目 - >属性 - >应用程序 - >装配信息对话框中手动更改装配版本。所以现在,每次我发布之前我都会将发布版本复制到汇编版本,所以我的对话框显示了当前版本的应用程序。必须有更好的方法来做到这一点。

我真正想做的就是拥有一个准确的,自动更新的,代码可访问的版本号。

这是我用来访问程序集版本号的代码:

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

另一种方法是找到访问发布版本的代码。

c# version clickonce build-automation
5个回答
22
投票

根据我的经验,sylvanaar的最后一行看起来像是要走的路;但需要注意的是,它仅适用于已部署的应用程序版本。出于调试目的,您可能需要以下内容:

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

        return "Debug";
    }

10
投票

我修改了我的.csproj文件来更新程序集版本。我为此创建了一个名为“Public Release”的配置,但不需要这样做。

  <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>

发布的版本可能是:

ApplicationDeployment.CurrentDeployment.CurrentVersion

3
投票

我修改了sylvanaar用于VB的解决方案:

- Microsoft.VisualBasic.targets instead of Microsoft.CSharp.targets
- CodeLanguage="VB" instead of CodeLanguage="CS" 
- AssemblyInfo.vb instead of VersionInfo.cs

,路径差异:

- $(SolutionDir).build instead of $(SolutionDir)Tools\MSBuildCommunityTasks
- $(ProjectDir)AssemblyInfo.vb instead of $(ProjectDir)Properties\VersionInfo.cs

,并删除条件:

- Condition="'$(BuildingInsideVisualStudio)' == 'true'"
- Condition="'$(BuildingInsideVisualStudio)|$(Configuration)' == 'true|PublicRelease'"

我还分别使用ClickOnce PublisherName和ProductName同步公司和产品,并根据当前日期生成版权:

- AssemblyCompany="$(PublisherName)"
- AssemblyProduct="$(ProductName)" 
- AssemblyCopyright="© $([System.DateTime]::Now.ToString(`yyyy`)) $(PublisherName)"

我最终将此添加到我的vbproj文件中。它需要首先安装MSBuildTasks NuGet包:

  <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />
  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <MSBuildCommunityTasksPath>$(SolutionDir).build</MSBuildCommunityTasksPath>
  </PropertyGroup>
  <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" Condition="'$(BuildingInsideVisualStudio)' == 'true'" />
  <Target Name="BeforeCompile">  
    <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
      <Output TaskParameter="OutputVersion" PropertyName="AssemblyVersionToUse" />
    </FormatVersion>
    <AssemblyInfo CodeLanguage="VB" OutputFile="$(ProjectDir)AssemblyInfo.vb" AssemblyVersion="$(AssemblyVersionToUse)" AssemblyFileVersion="$(AssemblyVersionToUse)" AssemblyCompany="$(PublisherName)" AssemblyProduct="$(ProductName)" AssemblyCopyright="© $([System.DateTime]::Now.ToString(`yyyy`)) $(PublisherName)"/>
  </Target>

我不确定项目文件中的位置有多重要,但我将其添加到项目文件的末尾,就在之前:

</Project>

3
投票

我想扩展Sylvanaar的答案,因为一些实施细节对我来说并不明显。所以:

  1. 手动安装在以下位置找到的社区构建任务:https://github.com/loresoft/msbuildtasks/releases注意:如果清理软件包,请不要通过nuget进行安装,因为构建将在有机会还原软件包之前失败,因为msbuildtasks在构建文件中被引用为任务。我把它们放在名为.build的解决方案文件旁边的文件夹中
  2. 将完全空的文件添加到名为VersionInfo.cs的项目属性文件夹中

3如果AssemblyInfo.cs中存在这些行,请将其删除

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

4修改csproj文件

  <!-- Include the build rules for a C# project. -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <!--INSERT STARTS HERE-->
  <!--note the use of .build directory-->
  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <MSBuildCommunityTasksPath>$(SolutionDir)\.build\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
  </PropertyGroup>
  <!-- Required Import to use MSBuild Community Tasks -->
  <Import Project="$(SolutionDir)\.build\MSBuild.Community.Tasks.targets" Condition="'$(BuildingInsideVisualStudio)' == 'true'" />
  <Target Name="BeforeCompile" Condition="'$(BuildingInsideVisualStudio)|$(Configuration)' == 'true|Release'">
    <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
      <Output TaskParameter="OutputVersion" PropertyName="AssemblyVersionToUse" />
    </FormatVersion>
    <AssemblyInfo CodeLanguage="CS" OutputFile="$(ProjectDir)Properties\VersionInfo.cs" AssemblyVersion="$(AssemblyVersionToUse)" AssemblyFileVersion="$(AssemblyVersionToUse)" />
  </Target>

5使用以下方法访问版本文本:

public string Version()
{
    Version version = null;

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
    }
    else
    {
        version = typeof(ThisAddIn).Assembly.GetName().Version;
    }

    return version.ToString();
}

2
投票

我反过来做了,使用通配符为我的汇编版本 - 1.0。* - 所以Visual Studio / MSBuild自动生成一个版本号:

// AssemblyInfo.cs    
[assembly: AssemblyVersion("1.0.*")]

然后我将以下AfterCompile目标添加到ClickOnce项目,以将PublishVersion与程序集版本同步:

<Target Name="AfterCompile">
    <GetAssemblyIdentity AssemblyFiles="$(IntermediateOutputPath)$(TargetFileName)">
      <Output TaskParameter="Assemblies" ItemName="TargetAssemblyIdentity" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <PublishVersion>%(TargetAssemblyIdentity.Version)</PublishVersion>
    </PropertyGroup>
</Target>
© www.soinside.com 2019 - 2024. All rights reserved.