2009-09-28 35 views
27

私のC#ClickOnceアプリケーションには、プロジェクトに自動インクリメントされたパブリッシュバージョンがあります。 - プロパティ - >タブを発行します。私のメニューにそのバージョンを表示したいと思いますヘルプ - >ボックスですが、明らかに私が使用しているコードはアセンブリバージョンにアクセスします。.NET ClickOnceアプリケーションで公開バージョンとアセンブリバージョンを同期させるにはどうすればよいですか?

アセンブリバージョンは、プロジェクトに手動で変更することができます - >プロパティ - >アプリケーション - >アセンブリ情報]ダイアログ。だから、私が公開する前に、パブリッシュバージョンをアセンブリバージョンにコピーしていたので、私のダイアログには現在のバージョンの が表示されています。これを行うにはより良い方法が必要です。

私が本当にやりたいことは、正確で自動更新された、コードにアクセスできるバージョン番号を持つことだけです。

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

代替が公開バージョンにアクセスするコードを見つけるために、次のようになります。

は、ここで私はアセンブリのバージョン番号にアクセスするために使用しているコードです。

答えて

19

sylvanaarの最後の行は、私の経験では、進むべき道のように見えます。ただし、アプリケーションのデプロイされたバージョンでのみ使用可能であるという注意点があります。 、

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

パスの違い:私はVBで使用するsylvanaarのソリューションを修正

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

     return "Debug"; 
    } 
+0

ありがとうございます - これは最も簡単な方法です。これは魅力的なように見えます! –

+3

アセンブリとビューのプロパティを右クリックすると、表示されるバージョンはまだAssemblyVersionなので、理想的ではありません。 –

+0

注:* ApplicationDeployment.IsNetworkDeployed *により、最初の例外がスローされます。これはデバッグなどを妨げる可能性があります。 –

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 
2

- $(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.*")] 

をそして私は、アセンブリのバージョンと同期のPublishVersionを割り当てるためのClickOnceプロジェクトに次のAfterCompileターゲットを追加しました:

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

Iだろう実装の詳細のいくつかが私には分かりませんでしたので、Sylvanaarの答えを広げるのが好きです。だから、:https://github.com/loresoft/msbuildtasks/releases注:あなたがあなたのパッケージをきれい場合nugetでインストールしないでください、msbuildtasksが参照されているので、ビルドは、パッケージを復元する機会を得る前に失敗するよう

  1. は手動で発見コミュニティビルドタスクをインストールビルドファイルのタスクとして使用します。私は

  2. プロジェクトに完全に空のファイルを追加します。プロパティが

    3 VersionInfo.cs

と呼ばれるフォルダ.buildと呼ばれる次のソリューションファイルをフォルダにこれらを入れて、彼らは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(); 
    } 
関連する問題