Shahar Gvirtz's Weblog

How-To: Use ClickOnce to deploy your Applications

Part 1 - What is ClickOnce?

CickeOnce, is a technology for deploying smart-client applications. When we talk about smart-client application that deployed with ClickOnce, we want that the application will:

  • Provide automatic installation in one click.
  • Install updated automatically
  • Can be installed from local file, or from the WEB.

ClickeOnce, give us this options out of the box, and all we need to do is to write two XML manifest files (one for the application, and one for the ClickOnce engine). If you use Visual Studio, you have a wizzard for this.
In this post, I'll show how to work with ClickOnce from Visual Studio, and from your code too.

Part 2 - How to use ClickOnce in your application?

First of all, ClickOnce supports deployment of Windows Applications from all types (Console image Applications, Windows Forms Applications, WPF Applications).
So, in this example I created a new empty Windows Forms Application. to edit the ClickOnce settings, you should go to the project properties page (by right click on the project name in the solution explorer, see pic. #1).

In the Property page go the the "Publish" tab. In this tab, you can change the ClickOnce settings for this Project. First of all, in the first textbox, you have to enter where VS will create the installation files. It can be in the local file system, web site imageand FTP.

If you'll click on the Application Files button, you can edit the files that will include in your  project.
You can add new files that are currently in your solution, and choose if they will be in the package.

 

 

image

 

In the Prerequisites screen, you can choose package that muse be installed in the computer before your project will deploy.

For example, the .NET version you use, SQL Server etc.

 

image

 

It's recommended to click on the Update button, and turn on the update feature.
By default, it's turned off. This feature give you the option to create new versions and the clients will automatically update.

By default, the updates should be in the same directory like the publish directory.
But, you can specify a special directory for the updates, if you want so. Note that the updater will check if there is a newer version in the server. the version defuned in the main screen, in the Publish tab in the Project properties. If the V in "Automatically increment revision with each publish" checked, then any publish will increase the version.

image

After you configure the Updates, click on the Options button in the main screen and configure the general details.

 

After you finish, go close this Dialog and click on the Publish button:

image

Now, Visual Studio will build your project and will publish it. After anything will done, a new IE window will open (by default, unless you change it in the Options screen.) with the product auto-generated page.image

The page that automatically generated, include the prerequisites, and if everything is already installed you can click in the "launch" link. After clicking, if you are verified publisher, the application will start automatically, and later can be started from the Start Menu shortcut.

 

 

 

Part 3 - How the updater works?

The current project includes only an empty form. Now, let's say it's version 1.0.0.1.
Now, I changed the back color of the form , and I want it to be version 2.0.0.0.

image image

I'll go to the Publish tab in the project properties and edit the version:

image

Now, I'll click the publish now button. It will publish the new version to the location I chose (in the web).
Next time I'll launch the application, as I set in the Update properties, the application will update and show this message:

image

If you click OK, then the new version will download and run.

Part 4 - ClickOnce with code

Until now, we worked with wizards and GUI to manage the ClickOnce deployment, but we can do it from our code too. First of all, we should add using statement to System.Deployment.Application:

using System.Deployment.Application;

Now, we can use the ApplicationDeployment class to manage our application deployment information. This code, for example, check for updates and shows a MessageBox with the version of the newest version for the current deployment. Note, that this code can replace the built-in message of new versions. you can cancel in the Update screen in the Publish tab the auto-check for updates, and do it manually from your code:

   1:  ApplicationDeployment deploy =   ApplicationDeployment.CurrentDeployment;
   2:  UpdateCheckInfo update = deploy.CheckForDetailedUpdate();
   3:  MessageBox.Show("You can update to version: " + update.AvailableVersion.ToString());

image

Note that this code will cause an exception if no update available, because then update.AvailableVersion.ToString() will be null. So, if you want to use this code, make sure that it's in if...else statement that show the message only if update doesn't equal to null.

We can fix this code a little, so the application will update after the message show:

ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
UpdateCheckInfo update = deploy.CheckForDetailedUpdate();
 if (deploy.CheckForUpdate())
{
     MessageBox.Show("You can update to version: " + update.AvailableVersion.ToString());
     deploy.Update();
     Application.Restart();
}

This code will work always. If an update is available, it will inform the user, download the update synchronously and restart the application. Otherwise, this code do nothing.

Put this code in the form load event, and then when you release a new version, the user will informed about and the application will update. you can use this code instead the built-in message, to make this progress more friendly.

Shahar Gvirtz.

Comments

Phil Winstanley said:

Can you do another post talking about provisioning IIS6 so that you can deploy directly to it?

# January 29, 2008 2:39 PM

shahargs said:

Yes, I'll write post like this later.

# January 30, 2008 12:30 AM

Reflective Perspective - Chris Alcock » The Morning Brew #22 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #22

# January 31, 2008 3:08 AM

GrandpaB said:

Shahar,

I have two questions concerning your Code example. First, where in my application do I insert the code, and will it be different for a VB application? My second question is, how can the user skip the program update?

# February 10, 2008 11:27 PM

shahargs said:

Grandpa,

The code is same for VB application except the difference in the syntax.

You can add YesNo MessageBox, and give the user the option to skip the update.

Shahar.

# February 11, 2008 12:30 AM

GrandpaB said:

Shahar,

Thanks for your speedy reply. I'm still uncertain where to insert the your code in my application.

# February 11, 2008 10:07 AM

shahargs said:

I wrote this code in the Form_Load event of the main form.

You can add "update" button or something like this and put your code there.

Shahar.

# February 11, 2008 11:18 AM

Anonymous said:

i wrote this code u=in my project, but i got following error:Application Identity is not set.

would you help me in solving the problem?

# July 12, 2008 1:20 AM

Khader said:

i did the update before application starts but update not working

# August 6, 2008 4:58 AM

alhambra eidos said:

Hi all

I am trying to do the same as I did with MageUI.exe but at the command

line, using Mage.exe.

In MageUI.exe I could specify an EntryPoint, and an IconFile, in the

Files section of the application manifest tab. I see no documentation

and no option in the help of mage.exe to do so from the command line.

Is it possible ?

How can I do that ?

Thanks in advance

Best regards,

In the manifest is like this:

<asmv1:assemblyIdentity name="AdministradorWin.exe" version="1.0.0.2" publicKeyToken="4b005ceeffd565b0" language="neutral" processorArchitecture="msil" type="win32" />

 <application />

 <entryPoint>

   <assemblyIdentity name="AdministradorWin.exe" version="1.0.0.2" publicKeyToken="4b005ceeffd565b0" language="neutral" processorArchitecture="msil" type="win32" />

   <commandLine file="AdministradorWin.exe.deploy" parameters="" />

 </entryPoint>

enrique.prados@a-e.es

# November 4, 2008 8:11 AM

Plater said:

In order to curb InvalidDeploymentException, I wrapped your code in a:

if (ApplicationDeployment.IsNetworkDeployed)

{ ... }

block. It seemed to prevent this code from trying to run when I attempt to debug

# November 11, 2008 1:54 PM

kjward said:

i've got clickonce working flawlessly on three neww apps, but i'd like to be able to somehow by-pass the "new update" message when there is a new version, so the user cannot skip the update...how to do that?

# December 9, 2008 8:20 AM

Gregg said:

Thanks for this post...it was very helpful!

The only problem I have now is trying to including the .NET Framework 3.5 SP1 files with my app. No matter what I do, I can't compile & Publish when I select the "Download prerequisites from the same location as my application" option. Any ideas?

# March 4, 2009 7:52 PM

mosafer from iran said:

hi sir,

thank you very much for your teach.

# March 8, 2009 8:13 AM

mosafer from iran said:

can i have your email if possible?

# March 8, 2009 8:16 AM

Static said:

Anyone have any clue on how to change the install directory!????

and, every update adds a new folder with all the files!! why doesnt it just replace???

# March 10, 2009 2:12 PM

kkrules said:

i am using the click once feature along with wix .the problem i am facing is that when i update the version of my installation, i am not getting the Update Available  screen that says that "A new version is available .do you want to download it now " when i use the URL .But when i run the application from the start menu (da 2nd time) this screen pops up .why is not being shown when im accessing the application from the URl(it is behaving like a new installation of the application )???

# March 17, 2009 6:51 AM

Kiyoshi said:

Hey. If you would be known, and not know, vegetate in a village; If you would know, and not be known, live in a city.

I am from Wales and too poorly know English, give true I wrote the following sentence: "Find last minute cheap fare, airline ticket and discount flight ticket for major cities of."

Best regards ;), Kiyoshi.

# April 5, 2009 2:15 AM

Raja Venkatesh said:

My service provider does not have frontpage extensions without which i'm unable to publish the program.  Is there any other way other than MSI which i currently use?

rrvenki@yahoo.com

# May 20, 2009 3:03 PM

wow leveling guide said:

My service provider does not have frontpage extensions without which i'm unable to publish the program.  Is there any other way other than MSI which i currently use?

# November 3, 2009 5:25 AM

orlando personal injury said:

this definitely makes thinks easier then i though. this is so good to use. thanks for the info.

# November 14, 2009 4:14 PM

Curry takeaway said:

Great tutorial - Thanks!

# November 19, 2009 10:12 AM

Ray said:

This is a great tutorial. I've installed ClickOnce for my latest app. download - but I would like to force people to download the latest update rather than opt for an earlier version. Can this be done?

<a href="www.gosimply.com/.../">Airport hotels with parking</a>

# November 23, 2009 10:18 AM

DVD Boxset said:

Using Clickonce to Deploy my applications?This definately making it pretty easier for us to work.Great tutorial on the overall,working very well on the first three new applications.How do i escape from the option to skip the updates on the beginning?Pls advise.Thanks.

# November 23, 2009 1:02 PM

Income Protection said:

This is a great piece of software. I've been using for a while to distribute my apps and it works like a charm.

# November 24, 2009 9:24 AM

Sergey said:

Thanks for a great post.

I have one question though:

Is it possible to specify the order in which prerequisites are installed? If Yes, then how?

Thank you.

# November 24, 2009 4:58 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)