Dave recently mentioned that he's looking for a way to display the published version of a ClickOnce application programatically. By now no doubt he's discovered it, but I thought I'd post it here, along with the code to programatically update an application.

(An aside: If you find this post useful, please click on the ad in the box over there in the sidebar on your right (under the Comicster box), and send a few cents in my direction. Ta.)

First, let's display the version in a label. I'm not sure what the version would do if the application wasn't installed with ClickOnce, so I'm not taking any chances:

label1.Text = "Comicster" + (ApplicationDeployment.IsNetworkDeployed ? 
    " " + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() : ""); 

You can see here that I'm testing whether the application was installed using ClickOnce before I check its version. The "ApplicationDeployment" class lives in the System.Deployment.Application namespace.

Later in my "Help|About" form I have a "Check for Updates" label, which checks the original install location for an updated version of the program, and installs it if it exists. Here's the code for that:

    if (ApplicationDeployment.IsNetworkDeployed)
    {
        Cursor cur = Cursor.Current;
        Cursor.Current = Cursors.WaitCursor;
        try
        {
            if (ApplicationDeployment.CurrentDeployment.CheckForUpdate())
            {
                Cursor.Current = cur;
                if (MessageBox.Show("An updated version is available. Would you like to update now?",
                    "Update Found", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    ApplicationDeployment.CurrentDeployment.Update();

                    Cursor.Current = cur;
                    MessageBox.Show("Update downloaded, Comicster will now restart.");
                    Application.Restart();
                }
            }
            else
            {
                Cursor.Current = cur;
                MessageBox.Show("No updates available at this time.");
            }
        }
        finally
        {
            Cursor.Current = cur;
        }
    } 

Again, pretty straight-forward. If the application is ClickOnce-deployed, I call CheckForUpdate(), and if an update was found I call Update().

This is truly seamless upgrading - the application restarts as the new version. No messing around. Amazing stuff.