简体   繁体   中英

How can I make my C# application check for updates?

I am building a C# windows application. I want it so whenever I click the update button in my form the application will Start looking for whether there is a new version avaliable on my Server.

If there is then proceed to update the Software.

How is this usually handled?

Take a look at Click Once . This thread might also make an interesting read.

Let me start by saying we offer a complete updating solution which includes:

wyUpdate handles all of the Vista/Windows 7 UAC problems and all the file permission problems that inevitably pop up when you're trying to update complex software.

That being said, if you want to build your own updater here are some tips:

Building your own updater

A good place to start is the wyUpdate C# source code I mentioned above. You can cannibalize it and use it for your own purposes. Some of the algorithms it contains:

  • Full Windows Vista / Windows 7 UAC support
  • Ability for limited users to check and then update if they have credentials
  • Support for wonky corporate inernet. (If you've ever worked with a corporation this is a real problem).
  • Quick extracting, patching, and installing of files.
  • Registry support.
  • Roll back files & registry on error or cancellation by the user
  • Self-update (no files left behind)

We also have the file specifications here .

Automatic updating

Since being automatic is a requirement let me tell you how we do it with our AutomaticUpdater control .

We use named pipes to communicate between the standalone updater (wyUpdate) and the Automatic Updater control sitting on your program's form. wyUpdate reports progress to the Automatic Updater, and the Automatic Updater can tell wyUpdate to cancel progress, to start downloading, start extracting, etc.

This keeps the updater separate from your application.

In fact, the exact named pipes C# code we use is included in an article I wrote a little while back: Multi-process C# app like Google Chrome .

If you want your app to be updated automatically from a website and handle the code by yourself do the following steps:

  1. Create an XML file with a unique name for example help.xml and build a structure to specify the list of files to be updated in specific directories and version and etc. Then upload them on your website.

  2. App after connecting to website downloads this help.xml file and reads the content to make sure there are any new files (update files) on the website...

  3. If a new version of files was existed so start downloading from URL specified in help.xml file!

Other answers look great.

However, if you're looking to hand-roll your own for whatever reason, simply put an XML file with information you need for your update process (eg description and version number of currently available version) somewhere on a webserver and use an HttpWebRequest (or HttpWebClient ?) to download this file and process like you would any XML.

I use this simple method in peSHIr Tweets and it works great. Just update this file after you put a new version online for download and your update check will find it. Anything about this process is changeable the way you like, as you wrote it yourself.

Unless this is a private project for your own amusement/use/learning - like in my case - do look if anything already available suits your needs though!

Take a look: Update Checker , I have wrote it to show the easy way to implement this feature in C#.

This XML file manages the updates:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myCoolApp>
    <currentVersion>
        <major>9</major>
        <minor>1</minor>
        <build>5</build>
    </currentVersion>
    <path>http://TestApp.exe</path>
</myCoolApp>

The main funtion Check4Update() reads the XML file and parse it:

XmlDocument oDom = new XmlDocument();
oDom.Load(_sXmlConfig);

string str = oDom.SelectSingleNode("//currentVersion/major").InnerText;
Int32.TryParse(str, out _nMajor);

str = oDom.SelectSingleNode("//currentVersion/minor").InnerText;
Int32.TryParse(str, out _nMinor);

str = oDom.SelectSingleNode("//currentVersion/build").InnerText;
Int32.TryParse(str, out _nBuild); 

_sNewVersionPath = oDom.SelectSingleNode("//path").InnerText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM