C # online upgrade

Source: Internet
Author: User
This is a WebService

Private appupdate. updateserv updatesvr;

private void button#click (Object sender, system. eventargs e)
{< br>
If (linkwebservices () = true)
{< br> This. label1.text = "connect to the server ....... pass ";
If (checkver () = true)
{< br> This. label2.text = "check the latest version and download ....... pass ";

}< br> else
{< br> This. label2.text = "check the latest version and download ....... fail ";
}< BR >}< br> else
{< br> This. label1.text = "connect to the server ....... fail ";
}< BR >}

// This is used to establish a connection with the upgraded Server
Private bool linkwebservices ()
{
Try
{
Updatesvr = new updateserv ();
Return true;
}
Catch
{
Return false;
}
}

// Call WebService to check whether the latest version is available
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 viewing 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;
// Judge the version number
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;
}
}
// The corresponding module is not 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 the new updateconfig. xml file and replace it after upgrade.
If (newversionexist)
{
Serverxmldoc. Save (temppath + "updateconfig. xml ");
If (dialogresult. Yes = MessageBox. Show ("Is there a new version, update? "," Prompt ", 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: supports backing up previous versions
System. Io. file. Delete (despath + filename );
}
// Todo: if the system is running, you must stop the system. You can use system. Diagnostics. Process to stop the system.
System. Io. file. Move (Dir, despath + filename );
}
MessageBox. Show ("upgraded ");
}
Else
{
// Todo: supports upgrading again
}
}
}
Catch (exception ex)
{
Throw new exception ("upgrade failed because:" + ex. Message, ex );
}
}

// Download the latest file

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.