C # download the new version of the program

Source: Internet
Author: User

To create a small program similar to the clickonce function, store program information in XML, and then determine whether the program version on the server is updated. If so, download the new version to the specified path and then call it. another problem is that I don't know how to close the update program.

The clickonce function of vs2005 is indeed good and easy to use.

Updatever. xml

<Updateinfo>
<Preversion> 1.0.0.1 </preversion>
<Updatepath> http: // localhost/testc1/apps/</updatepath>
<Currentpath> C:/backup/my projects/updatever/bin/debug/update/</currentpath>
<Exefile> testc1.exe </exefile>
<Files>
<File> c1.common. dll </File>
<File> c1.win. c1input. dll </File>
<File> c1.win. c1list. dll </File>
<File> c1.win. c1truedbgrid. dll </File>
<File> dbconfig. xml </File>
<File> devcomponents. dotnetbar. dll </File>
</Files>
</Updateinfo>

Updatever. CS

Using system;
Using system. Collections. Generic;
Using system. text;

Using system. reflection;
Using system. net;
Using system. xml;
Using system. IO;
Using system. diagnostics;

Namespace updatever
{
Class updaetver
{
Public String prever;
Public String curver;
Public String updatepath;
Public String curpath;
Public String exefilename;
Public String [] files;

Public void loadxml ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load ("updatever. xml ");
Xmlnode node = Doc. childnodes [1];
Prever = node. childnodes [0]. innertext;
Updatepath = node. childnodes [1]. innertext;
Curpath = node. childnodes [2]. innertext;
Exefilename = node. childnodes [3]. innertext;
Int filecount = node. childnodes [4]. childnodes. count;
Files = new string [filecount];
For (INT I = 0; I <filecount; I ++)
{
Files [I] = node. childnodes [4]. childnodes [I]. innertext;
}
}

Public void savexml ()
{
Xmldocument Doc = new xmldocument ();
Doc. Load ("updatever. xml ");
Xmlnode node = Doc. childnodes [1];
Node. childnodes [0]. innertext = curver;
Doc. Save ("updatever. xml ");
}

Public bool checkversion ()
{
Loadxml ();
Assembly A = assembly. loadfrom (updatepath + exefilename );
Version newver = A. getname (). version;
Curver = newver. tostring ();
Version oldver = new version (PreveR );
Int check = newver. compareto (oldver );
If (check> 0)
Return true;
Else
Return false;
}

Public void updateversion ()
{
If (! Directory. exists (curpath ))
{
Directory. createdirectory (curpath );
}

WebClient = new WebClient ();
String exefilename = exefilename;
WebClient. downloadfile (updatepath + exefilename, curpath + exefilename );
For (INT I = 0; I <files. length; I ++)
{
String filename = files [I];
Console. writeline (filename );
WebClient. downloadfile (updatepath + filename, curpath + filename );
}
}

Public void runapplication ()
{
If (file. exists (curpath + exefilename ))
{
Processstartinfo info = new processstartinfo (curpath + exefilename );
Info. workingdirectory = curpath;
Process. Start (Info );
}

}

Public void Update ()
{
Bool check = checkversion ();
If (check = true)
{
Updateversion ();
Savexml ();
}
Runapplication ();
}
}
}

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.