【问题标题】:How to synchronise the publish version to the assembly version in a .NET ClickOnce application?如何在 .NET ClickOnce 应用程序中将发布版本与程序集版本同步?
【发布时间】:2009-09-28 19:30:02
【问题描述】:

在我的 C# ClickOnce 应用程序中,Project -> Properties -> Publish 选项卡中有一个自动递增的发布版本。我想在我的菜单 Help -> About 框中显示该版本,但我使用的代码显然访问了程序集版本,这是不同的。

可以在 Project -> Properties -> Application -> Assembly Information 对话框中手动更改程序集版本。所以现在,每次我发布之前,我一直在将发布版本复制到程序集版本,所以我的对话框显示了当前版本 应用程序。必须有更好的方法来做到这一点。

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

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

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

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

【问题讨论】:

    标签: c# version clickonce build-automation


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢 - 这是最简单的方法,而且看起来很有魅力!
    • 这并不理想,因为当您右键单击一个程序集并查看属性时,显示的版本仍然是 AssemblyVersion。
    • 注意:ApplicationDeployment.IsNetworkDeployed 将导致第一次机会异常被抛出。这可能会干扰调试等。
    • 使用 if (!Debugger.IsAttached && ApplicationDeployment.*) {...} 在从 VisualStudio 调试 ClickOnce 应用时避免第一次机会异常。
    • 添加 if (ApplicationDeployment.IsNetworkDeployed) 似乎可以防止任何调试异常,也许这在 VS 2015 中是新的。我只是想我会分享讨论和/或帮助他人。没有它,应用程序每次都会抛出异常。为了以防万一,添加 try catch 可能不是一个坏主意,
    【解决方案2】:

    我修改了我的 .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 的答案,因为一些实施细节对我来说并不明显。所以:

      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();
      }
      

      【讨论】:

        【解决方案4】:

        我修改了 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'"
        

        我还将 Company 和 Product 分别与 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>
        

        【讨论】:

          【解决方案5】:

          我反其道而行之,为我的程序集版本使用了通配符 - 1.0.* - 所以 Visual Studio/MSBuild 自动生成了一个版本号:

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

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

          <Target Name="AfterCompile">
              <GetAssemblyIdentity AssemblyFiles="$(IntermediateOutputPath)$(TargetFileName)">
                <Output TaskParameter="Assemblies" ItemName="TargetAssemblyIdentity" />
              </GetAssemblyIdentity>
              <PropertyGroup>
                <PublishVersion>%(TargetAssemblyIdentity.Version)</PublishVersion>
              </PropertyGroup>
          </Target>
          

          【讨论】:

          • 我按照这个,但是更改了 ApplicationVersion ,它似乎可以工作,但网站的版本号错误。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 2018-08-11
          • 1970-01-01
          • 1970-01-01
          • 2011-07-05
          相关资源
          最近更新 更多