ClickOnce DIY automatic update download upgrade self-implementation

Source: Internet
Author: User

The concept of SmartClient has been quite popular recently, but a large number of software has been doing so before Microsoft proposed this term. On the one hand, it is to simplify the deployment of the client, and on the other hand, it provides the automatic upgrade function; for traditional WinForm applications, it is indeed a good solution to reduce maintenance costs;
When Microsoft launched the SmartClient concept, it launched the related updater Application Block, which is quite good. However, the author wrote a simple implementation based on the software features in the previous section, you may also understand the principles:
The author's simplified version automatically upgrades the manager in four steps:
1. A local class responsible for searching for and downloading the new version
2. Is it hard-coded in the local configuration file (or in the code? Not good), pointing to the URL of the Update Server
3. a configuration file on the server that identifies the version number and the URL of the new file
4. Call example
1. Version Management
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"); // the URL of the configuration file in the network obtained from the local configuration file
Request. Proxy = WebProxy. getdefaproxy Proxy ();
Request. GetResponse (); // If a response can be obtained, the network is normal.
}
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 (); // This URL will be automatically downloaded in the future
Sys. UpdatePage = GetValue (doc, "DownloadPage"). Trim (); // If Automatic download fails, manual download is provided on this page.
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; // if the value is greater than 0, a new Version is released.
} // Load the network configuration file, read some configuration parameters that do not want to be placed 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); // you can customize the read configuration file.
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;
Do
{
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 configuration files may include:
<Configuration>
<Deleetask>
<Add key = "UpdateUrl" value = "http://www.abc.com/download/release.xml"/>
</AppSettings>
</Configuration>
3. The network configuration file may be:
<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" type = "codeph" text = "/codeph"/>
</App>
</Config>
4. Call example
Start a background thread at an appropriate time (for example, when an application is started:
Thread thread = new Thread (new ThreadStart (threadMethodUpdate ));
Thread. Start ();

Private void threadMethodUpdate ()
{

If (VersionAgent. CheckNetwork () // The network is normal.
{
If (VersionAgent. CheckUpdate () // check for updates and obtain Network Parameters
{
If (VersionAgent. goUpdate () // obtain the new version (because my software is small, I downloaded the new version without prompting the user. If I think it is inappropriate, I can use MessageBox to prompt you)
{
MessageBox. Show ("the updated version of the product is detected and will be automatically updated soon! "," Version upgrade ", MessageBoxButtons. OK, MessageBoxIcon. Information );
System. Diagnostics. Process. Start (Sys. UpdateFile );
System. Environment. Exit (0 );
}
Else
{
MessageBox. Show ("the system detects an updated version, but the automatic download fails. Click" OK "," version upgrade ", MessageBoxButtons. OK, MessageBoxIcon. Error );
System. Diagnostics. Process. Start (Sys. UpdatePage );
System. Environment. Exit (0 );
}
}
}
Else // You can do nothing
MessageBox. Show ("unable to connect to the server for automatic upgrade! \ N check the network connection "+ Pub. GetSetting (" UpdateUrl ")," network exception ", MessageBoxButtons. OK, MessageBoxIcon. Warning );
}

Related Article

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.