How to display ClickOnce Version number on Windows Forms

后端 未结 9 717
眼角桃花
眼角桃花 2020-12-23 11:32

I have a windows forms application that is deployed to two different locations.

  • Intranet - ClickOnce
  • Internet - Installed on a citrix farm through Win
相关标签:
9条回答
  • 2020-12-23 11:51

    Hard code, or... Keep track on your versions (File, Assembly, Deploy) in a database. Make a call to the database with your Assembly and get the Deploy version.

    This assumes that you are incrementing your versions in a logical way such that each version type has a relationship. It's a lot of work for such a minor problem. I'd personally go with Jared's solution; although I hate hard coding anything.

    0 讨论(0)
  • 2020-12-23 11:55

    Try thread verification:

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        if (ApplicationDeployment.CurrentDeployment.CurrentVersion != ApplicationDeployment.CurrentDeployment.UpdatedVersion)
        {
            Application.ExitThread();
            Application.Restart();
        }
    }
    
    0 讨论(0)
  • 2020-12-23 11:55

    To expand on RobinDotNet's solution:

    Protip: You can automatically run a program or script to do this for you from inside the .csproj file MSBuild configuration every time you build. I did this for one Web application that I am currently maintaining, executing a Cygwin bash shell script to do some version control h4x to calculate a version number from Git history, then pre-process the assembly information source file compiled into the build output.

    A similar thing could be done to parse the ClickOnce version number out of the project file i.e., Project.PropertyGroup.ApplicationRevision and Project.PropertyGroup.ApplicationVersion (albeit I don't know what the version string means, but you can just guess until it breaks and fix it then) and insert that version information into the assembly information.

    I don't know when the ClickOnce version is bumped, but probably after the build process so you may need to tinker with this solution to get the new number compiled in. I guess there's always /*h4x*/ +1.

    I used Cygwin because *nix scripting is so much better than Windows and interpreted code saves you the trouble of building your pre-build program before building, but you could write the program using whatever technology you wanted (including C#/.NET). The command line for the pre-processor goes inside the PreBuildEvent:

    <PropertyGroup>
      <PreBuildEvent>
        $(CYGWIN_ROOT)bin\bash.exe --login -c refresh-version
      </PreBuildEvent>
    </PropertyGroup>
    

    As you'd imagine, this happens before the build stage so you can effectively pre-process your source code just before compiling it. I didn't want to be automatically editing the Properties\AssemblyInfo.cs file so to play it safe what I did was create a Properties\VersionInfo.base.cs file that contained a text template of a class with version information and was marked as BuildAction=None in the project settings so that it wasn't compiled with the project:

    using System.Reflection;
    using EngiCan.Common.Properties;
    
    [assembly: AssemblyVersion("0.$REVNUM_DIV(100)$.$REVNUM_MOD(100)$.$DIRTY$")]
    [assembly: AssemblyRevisionIdentifier("$REVID$")]
    

    (A very dirty, poor-man's placeholder syntax resembling Windows' environment variables with some additional h4x thrown in was used for simplicity's/complexity's sake)

    AssemblyRevisionIdentifierAttribute was a custom attribute that I created to hold the Git SHA1 since it is much more meaningful to developers than a.b.c.d.

    My refresh-version program would then copy that file to Properties\VersionInfo.cs, and then do the substitution of the version information that it already calculated/parsed (I used sed(1) for the substitution, which was another benefit to using Cygwin). Properties\VersionInfo.cs was compiled into the program. That file can start out empty and you should ignore it by your version control system because it is automatically changing and the information to generate it is already stored elsewhere.

    0 讨论(0)
提交回复
热议问题