2009-09-28 68 views
27

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

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

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

下面是我使用访问组件的版本号代码:

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

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

回答

19

希尔瓦纳的最后一行看起来像是要走的路,在我的经验;但有一点需要注意的是,它只适用于应用程序的部署版本。为了进行调试,你可能想是这样的:

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

     return "Debug"; 
    } 
+0

非常感谢 - 这是最简单的方法,它看起来像一个魅力工作! – 2009-09-30 01:05:35

+3

虽然这并不理想,因为当您右键单击程序集并查看属性时,显示的版本仍然是AssemblyVersion。 – 2009-12-17 14:01:06

+0

注意:* ApplicationDeployment.IsNetworkDeployed *将导致引发第一次机会异常。这可能会干扰调试等。 – 2014-06-25 22:56:59

10

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

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

我修改希尔瓦纳的解决方案为使用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和产品名称分别生成基于当前日期版权所有:

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

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

  1. 手动安装在发现社会的构建任务:https://github.com/loresoft/msbuildtasks/releases注意:如果你清理你的包,作为构建会得到一个机会来恢复包之前失败,因为msbuildtasks引用不要安装的NuGet作为构建文件中的任务。我把这些文件夹旁边叫.build

  2. 解决方案文件完全空文件添加到您的项目,如果他们中的AssemblyInfo.cs

    存在属性文件夹名为VersionInfo.cs

3删除这些行

[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(); 
    } 
相关问题