How to synchronise the publish version to the assembly version in a .NET ClickOnce application?

后端 未结 5 1411
甜味超标
甜味超标 2020-12-14 01:10

In my C# ClickOnce application, there is an auto-incremented publish version in the Project -> Properties -> Publish tab. I\'d like to display that version

5条回答
  •  情歌与酒
    2020-12-14 01:40

    I would like to expand on Sylvanaar's answer, as some of implementation details weren't obvious to me. So:

    1. Manually install community build tasks found at: https://github.com/loresoft/msbuildtasks/releases Note: Don't install by nuget if you clean your packages, as the build will fail before getting a chance to restore the packages, since msbuildtasks are referenced as a task in the build file. I put these in folder next to solution file called .build

    2. Add a completely empty file to your projects properties folder called VersionInfo.cs

    3 Remove these lines if they exist in AssemblyInfo.cs

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

    4 Modify your csproj file

      
      
    
      
      
      
        $(SolutionDir)\.build\MSBuildCommunityTasks
      
      
      
      
        
          
        
        
      
    

    5 Use a method like the following to access the version text:

    public string Version()
    {
        Version version = null;
    
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
        }
        else
        {
            version = typeof(ThisAddIn).Assembly.GetName().Version;
        }
    
        return version.ToString();
    }
    

提交回复
热议问题