2009-09-28 2 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에 대해 와일드 카드를 사용했습니다.

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

을 그리고 나는 어셈블리 버전으로 동기화 PublishVersion을 할당 ClickOnce를 프로젝트에 다음 AfterCompile 대상을 추가 : - * 그래서 비주얼 스튜디오/MSBuild를 자동으로 버전 번호를 생성

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

나는 것 Sylvanaar의 대답을 확장하고 싶습니다. 구현 세부 사항 중 일부는 분명하지 않았습니다. 그래서 : https://github.com/loresoft/msbuildtasks/releases 주 :

  1. 수동에서 찾을 커뮤니티 빌드 작업을 설치 빌드가 패키지를 복원 할 수있는 기회를 얻기 전에 실패로 msbuildtasks가 참조하고 있기 때문에 당신이, 당신의 패키지를 청소하는 경우 nuget에 의해 설치하지 마십시오 빌드 파일에서 작업으로. 나는 그들이있는 AssemblyInfo.cs

    을에 존재하는 경우 속성 VersionInfo.cs

3이 줄을 제거라는 폴더 프로젝트에 완전히 빈 파일을 추가 다음

  • .build라는 솔루션 파일을 폴더에 다음을 넣어

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