Implementing online upgrades with C #

Source: Internet
Author: User
Tags foreach bool connect split tostring
Online

Implementing online upgrades with C #


This is a WebService

Private Appupdate.updateserv Updatesvr;


private void Button1_Click (object sender, System.EventArgs e)
{

if (Linkwebservices () ==true)
{
this.label1.text= "Connect server .... Pass ";

if (Checkver () ==true)
{
this.label2.text= "Check the latest version and download ... Pass ";

}
Else
{
this.label2.text= "Check the latest version and download ... FAIL ";
}
}
Else
{
this.label1.text= "Connect server .... FAIL ";
}
}

This is used to establish a connection with the upgrade server
private bool Linkwebservices ()
{
Try
{
Updatesvr=new Updateserv ();
return true;
}
Catch
{
return false;
}
}

Call WebService to check if there is an updated version
private bool Checkver ()
{
string path =application.startuppath;
Try
{
Versioncheck (path);
return true;
}
catch (Exception ex)
{
MessageBox.Show (ex. ToString ());
return false;
}
}

private void Versioncheck (string despath)
{
Try
{
#region View files and directories
if (!despath.endswith (@ "\"))
Despath = @ "\";

if (! System.IO.Directory.Exists (Despath))
{
System.IO.Directory.CreateDirectory (Despath);
}

String TempPath = Despath + @ "tempdespathcache\";

if (System.IO.Directory.Exists (TempPath))
{
System.IO.Directory.Delete (temppath,true);
System.IO.Directory.CreateDirectory (TempPath);
}
Else
System.IO.Directory.CreateDirectory (TempPath);

if (! System.IO.File.Exists (Despath + "Updateconfig.xml"))
{
System.Xml.XmlDocument updateconfig = new System.Xml.XmlDocument ();
Updateconfig.loadxml (@ "<root></root>");
Updateconfig.save (Despath + "Updateconfig.xml");
}
#endregion


System.Xml.XmlDocument Serverxmldoc = Updatesvr.appupdatevertion ();
System.Xml.XmlDocument Localxmldoc = new System.Xml.XmlDocument ();
Localxmldoc.load (Despath + "Updateconfig.xml");
BOOL Newversionexist = false;
BOOL Moduleexist = false;
System.Xml.XmlNode serverNode0 = serverxmldoc.childnodes[0];
System.Xml.XmlNode localNode0 = localxmldoc.childnodes[0];
foreach (System.Xml.XmlNode servernode in serverNode0)
{
Moduleexist = false;
foreach (System.Xml.XmlNode localnode in localNode0)
{
Find the corresponding module
if (localnode.childnodes[0). InnerText = = Servernode.childnodes[0]. InnerText)
{
Moduleexist = true;
Version number judgment
if (Localnode.childnodes[1].innertext.compareto (servernode.childnodes[1). InnerText) < 0)
{
Newversionexist = true;
if (system.configuration.configurationsettings.appsettings["Netstyle"]. ToString () = "Internet")
{
DownloadFile (Servernode.childnodes[2]. Innertext,temppath + servernode.childnodes[0]. InnerText);
}
Else
{
DownloadFile (Servernode.childnodes[3]. Innertext,temppath + servernode.childnodes[0]. InnerText);
}
}
Break
}
}
No corresponding module found
if (false = = Moduleexist)
{

if (system.configuration.configurationsettings.appsettings["Netstyle"]. ToString () = "Internet")
{
DownloadFile (Servernode.childnodes[2]. Innertext,temppath + servernode.childnodes[0]. InnerText);
}
Else
{
DownloadFile (Servernode.childnodes[3]. Innertext,temppath + servernode.childnodes[0]. InnerText);
}
}
}
Write new Updateconfig.xml replace after upgrade complete
if (newversionexist)
{
Serverxmldoc.save (TempPath + "Updateconfig.xml");
if (Dialogresult.yes = = MessageBox.Show ("New version, update?", "hint", Messageboxbuttons.yesno))
{
string[] dirs = System.IO.Directory.GetFiles (TempPath, "*.*");
String FileName;
foreach (String dir in dirs)
{
FileName = (dir. Split (Convert.tochar (@))) [Dir. Split (Convert.tochar (@ ")). Length-1]);
if (System.IO.File.Exists (Despath + fileName))
{
TODO: Backup Previous versions can be supported
System.IO.File.Delete (Despath + fileName);
}
TODO: If the system is running, you have to stop the system, as to how to stop, you may be able to use System.Diagnostics.Process
System.IO.File.Move (Dir,despath + fileName);
}
MessageBox.Show ("upgrade completed");
}
Else
{
TODO: Can support a prompt upgrade
}
}
}
catch (Exception ex)
{
throw new Exception ("Upgrade failed because of:" + ex.) MESSAGE,EX);
}
}

Download the latest files

private void DownloadFile (String source,string fileName)
{
Try
{
System.Net.WebClient mywebclient = new System.Net.WebClient ();
Mywebclient.downloadfile (Source,filename);
}
catch (Exception ex)
{
throw new Exception ("Download failed because:" + ex.) MESSAGE,EX);
}
}



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.