ClickOnce DIY Automatic Update download upgrade self-implementation _ practical skills

Source: Internet
Author: User
Tags file url
SmartClient concept is relatively hot recently, but before Microsoft put forward this term has a lot of software in doing so, on the one hand is to simplify the deployment of clients, on the one hand is to provide automatic upgrade function; For traditional WinForm applications, it is a good solution to reduce maintenance costs. ;
Microsoft in the introduction of the SmartClient concept, the introduction of the relevant Updater Application block, did a good job, but the author of the previous paragraph or according to the software characteristics of their own wrote a very simple implementation, we can probably understand the principle:
The author's simplified version of the automatic upgrade Manager requires only four steps:
1. A local class that is responsible for finding and downloading a new version
2. In the local configuration file (or hard-coded in code?) Not so good.), point to the URL of the update server
3. Server previous configuration file that identifies the version number and the new file URL
4. Call Example
1. Version Management class
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Xml;
Using System.Net;
Using System.IO;
Using System.Windows.Forms;
Namespace Survey
{
Class Versionagent
{
public static bool Checknetwork ()
{
HttpWebRequest request;
Try
{
Request = (HttpWebRequest) webrequest.create (pub.getsetting ("UpdateUrl"))//URL of the configuration file in the network obtained from the local profile
Request. Proxy = Webproxy.getdefaultproxy ();
Request. GetResponse ()//If you can get a response, the network is fine
}
catch (Exception e)
{
Pub.logerror (e);
return false;
}
return true;
}

public static bool Checkupdate ()
{
XmlDocument doc = loadxmldocument (pub.getsetting ("UpdateUrl"));
Sys.updateurl = GetValue (Doc, "DownloadURL"). Trim ()//will be automatically downloaded with this URL in the future
Sys.updatepage = GetValue (Doc, "Downloadpage"). Trim ()///If automatic download fails, will be provided to this page by hand download
String warningrate = GetValue (Doc, "Warningrate"). Trim ();
Float. TryParse (Warningrate,out sys.warningrate);
String netversion = GetValue (Doc, "Version"). Trim ();
Version localversion=system.reflection.assembly.getexecutingassembly (). GetName (). Version;
return new Version (netversion). CompareTo (new version (localversion)) >0;//greater than 0 indicates a newer release
}//This method is to load the network configuration file, read some configuration parameters that you do not want to place locally, and compare the local and network version numbers
public static bool Goupdate ()
{
Return DownLoadFile (Sys.updatefile,sys.updateurl);

}
public static string GetValue (XmlDocument doc, string Key)
{
String Value;
Try
{
XmlElement elem = (XmlElement) doc. selectSingleNode (@ "/config/app/" + Key);//Read configuration file can be defined by itself
Value = Elem = null? "": Elem. GetAttribute ("value");
}
Catch
{
Value = "";
}
return Value;
}
public static XmlDocument loadxmldocument (string filenameorurl)
{
XmlDocument doc = null;
Try
{
doc = new XmlDocument ();
Doc. Load (Filenameorurl);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show (E.message);
Pub.logerror (e);
doc = null;
}
return doc;
}

public static bool DownLoadFile (string FileName, String Url)
{
BOOL Value = false;
WebResponse response = null;
Stream stream = null;
Try
{
HttpWebRequest request = (HttpWebRequest) webrequest.create (URL);
Response = Request. GetResponse ();
Stream = Response. GetResponseStream ();
if (!response. Contenttype.tolower (). StartsWith ("text/"))
{
Value = Savebinaryfile (response, FileName);
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show (E.message);
Pub.logerror (e);
}
return Value;
}

private static bool Savebinaryfile (WebResponse response, String FileName)
{
bool Value = true;
byte[] buffer = new byte[1024];
Try
{
if (file.exists (FileName))
File.delete (FileName);
Stream OutStream = System.IO.File.Create (FileName);
Stream instream = Response. GetResponseStream ();
int l;
Todo
{
L = instream.read (buffer, 0, buffer. Length);
if (L > 0)
Outstream.write (buffer, 0, L);
}
while (L > 0);
Outstream.close ();
Instream.close ();
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show (E.message);
Pub.logerror (e);
Value = false;
}
return Value;
}
}
}
2. Local profiles may be as follows:
<configuration>
<appSettings>
<add key= "UpdateUrl" value= "Http://www.abc.com/download/release.xml"/>
</appSettings>
</configuration>
3. Network configuration files may be as follows:
<config>
<app>
<version value= "1.1.9.2"/>
<releasedate value= "2006-12-12"/>
<downloadpage value= "http://www.abc.com/download/index.htm"/>
<downloadurl value= "Http://www.abc.com/download/update.exe"/>
<warningrate value= "0.3"/>
</app>
</config>
4. Call Example
Start a background thread to work at the right time (for example, when the application starts):
Thread thread = new Thread (new ThreadStart (threadmethodupdate));
Thread. Start ();

private void Threadmethodupdate ()
{

if (Versionagent.checknetwork ())//Network condition is normal
{
if (Versionagent.checkupdate ())//Check for updates and get network parameters
{
if (Versionagent.goupdate ())//Get a new version of this (because my software is very small, so do not prompt the user's situation on the new version of the download, if you think it is inappropriate, can be prompted by MessageBox)
{
MessageBox.Show ("The updated version of the product is detected, the automatic update is about to begin!") "," Version Upgrade ", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Diagnostics.Process.Start (Sys.updatefile);
System.Environment.Exit (0);
}
Else
{
MessageBox.Show ("The system detected a newer version, but the automatic download failed, click OK for manual Download", "Version Upgrade", MessageBoxButtons.OK, Messageboxicon.error);
System.Diagnostics.Process.Start (Sys.updatepage);
System.Environment.Exit (0);
}
}
}
ELSE//can also not hint at anything
MessageBox.Show ("Unable to connect to the server for automatic upgrades!") \ nplease check the network connection "+ pub.getsetting (" UpdateUrl ")," Network Anomaly ", MessageBoxButtons.OK, messageboxicon.warning);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.