Smart upgrade is also simple (using web services to create upgrade programs)

Source: Internet
Author: User

Original article: http://blog.csdn.net/chendazhi/archive/2006/05/09/715542.aspx
UpgradeProgramThere are multiple ways to create, the following describes an upgrade program made with Web Services. Upgrading programs through web services is as simple as reading and writing local files. So I will give it directly.Code.

 

Some Web Services Code:

Using system;

Using system. Web;

Using system. Web. Services;

Using system. Web. Services. Protocols;

Using system. IO;

 

[WebService (namespace = "http://tempuri.org/")]

[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]

Public class service: system. Web. Services. WebService

{

Public Service ()

{

// If you use the designed component, uncomment the following line

// Initializecomponent ();

}

/// <Summary>

/// Server path for file upgrade

/// </Summary>

Private const string updateserverpath = "D: \ debug ";

[Webmethod (description = "return the program version number on the server")]

Public String serverver ()

{

Return "4.0 ";

}

[Webmethod (description = "returns the file to be updated")]

Public String [] newfiles ()

{

Directoryinfo di = new directoryinfo (updateserverpath );

Fileinfo [] Fi = Di. getfiles ();

Int intfiles = Fi. length;

String [] mynewfiles = new string [intfiles];

Int I = 0;

Foreach (fileinfo fitemp in FI)

{

Mynewfiles [I] = fitemp. Name;

System. Diagnostics. Debug. writeline (fitemp. Name );

I ++;

}

 

Return mynewfiles;

}

[Webmethod (description = "returns the size of the file to be updated")]

Public int allfilesize ()

{

Int filesize = 0;

String [] files = directory. getfiles (updateserverpath );

Foreach (string file in files)

{

Fileinfo myinfo = new fileinfo (File );

Filesize + = (INT) myinfo. Length/1024;

}

Return filesize;

}

 

[Webmethod (description = "returns the byte array of the given file")]

Public byte [] getnewfile (string requestfilename)

{

/// Obtain a file on the server

If (requestfilename! = NULL | requestfilename! = "")

Return getbinaryfile (updateserverpath + "\" + requestfilename );

Else

Return NULL;

}

 

/// <Summary>

/// Return the byte array of the given file path.

/// </Summary>

/// <Param name = "FILENAME"> </param>

/// <Returns> </returns>

Private byte [] getbinaryfile (string filename)

{

If (file. exists (filename ))

{

Try

{

// Open an existing file for reading.

Filestream S = file. openread (filename );

Return convertstreamtobytebuffer (s );

}

Catch

{

Return new byte [0];

}

}

Else

{

Return new byte [0];

}

}

/// <Summary>

/// Convert the given file flow into a binary byte array.

/// </Summary>

/// <Param name = "thestream"> </param>

/// <Returns> </returns>

Private byte [] convertstreamtobytebuffer (system. Io. Stream thestream)

{

Int B1;

System. Io. memorystream tempstream = new system. Io. memorystream ();

While (b1 = thestream. readbyte ())! =-1)

{

Tempstream. writebyte (byte) B1 ));

}

Return tempstream. toarray ();

}

 

}

 

Upgrade Code:

Using system;

Using system. Collections. Generic;

Using system. componentmodel;

Using system. Data;

Using system. drawing;

Using system. text;

Using system. Windows. forms;

Using system. Threading;

Using system. xml;

Using system. IO;

Using system. diagnostics;

 

Namespace AutoUpdate

{

Public partial class frmautoupdate: Form

{

Public frmautoupdate ()

{

Initializecomponent ();

}

/// <Summary>

/// Current version

/// </Summary>

Private string m_strlocalver;

/// <Summary>

/// Server version

/// </Summary>

Private string m_strserverver;

/// <Summary>

/// Total number of files to be updated

/// </Summary>

Private int m_intfilescount = 0;

/// <Summary>

/// File size to be updated

/// </Summary>

Private int m_allfilesize;

/// <Summary>

/// The size of the file being downloaded

/// </Summary>

Private int m_downingfilesize;

/// <Summary>

/// Size of the downloaded file

/// </Summary>

Private int m_downedfilesize;

/// <Summary>

/// Array, the file to be updated

/// </Summary>

Private string [] m_files = NULL;

/// <Summary>

/// Define the file object

/// </Summary>

Webreference. Service ws;

/// <Summary>

/// Connect to webserveces and download the file thread

/// </Summary>

Private thread downthread;

Private void frmautoupdata_load (Object sender, eventargs E)

{

Downthread = new thread (New threadstart (startupdate ));

Downthread. Start ();

}

/// <Summary>

/// Connect to the server and start upgrading

/// </Summary>

Private void startupdate ()

{

Ws = new webreference. Service ();

M_allfilesize = ws. allfilesize ();

Try

{

M_strlocalver = config. Ver; // read the current version number from the configuration file

M_strserverver = ws. serverver (); // Save the server version number

 

Begininvoke (new system. eventhandler (updateui), lbllink );

// If the current version is earlier than the server version, update it...

If (double. parse (m_strlocalver) <double. parse (m_strserverver ))

{

Begininvoke (new system. eventhandler (updateui), lbldownload );

M_files = ws. newfiles (); // file to be updated

If (m_files! = NULL)

{

M_intfilescount = m_files.length;

For (INT I = 0; I <= m_intfilescount-1; I ++)

{

Downfile (m_files [I]);

Debug. writeline (m_files [I]);

}

// Config. Ver = strserverver; // record the version number to the configuration file

}

}

Else

{

Begininvoke (New eventhandler (onshowmessage ),

"You have installed the latest version on your computer. You do not need to upgrade it .");

Return;

}

}

Catch (exception ex)

{

Begininvoke (New eventhandler (onshowmessage), Ex. Message );

Return;

}

// Set the label attribute in the UI thread

Begininvoke (new system. eventhandler (updateui), lblstartexe );

// Installation File

Methodinvoker misetup = new methodinvoker (this. startsetup );

This. begininvoke (misetup );

}

/// <Summary>

/// Download an object

/// </Summary>

/// <Param name = "FILENAME"> file name </param>

Private void downfile (string filename)

{

// Obtain the byte array of the binary file;

Byte [] bt = ws. getnewfile (filename );

M_downingfilesize = Bt. Length/1024;

String strpath = application. startuppath + "\ update \" + filename;

If (file. exists (strpath ))

{

File. Delete (strpath );

}

Filestream FS = new filestream (strpath, filemode. createnew );

FS. Write (BT, 0, BT. Length );

FS. Close ();

}

/// <Summary>

/// Start to install the downloaded file

/// </Summary>

Private void startsetup ()

{

// End the currently running main program

Process [] pros = process. getprocesses ();

For (INT I = 0; I <pros. length; I ++)

{

If (pros [I]. processname = "instructor attendance system ")

{

Pros [I]. Kill ();

}

}

// Start copying objects

For (INT I = 0; I <= m_intfilescount-1; I ++)

{

String sourcefilename = application. startuppath + "\ update \" + m_files [I];

String destfilename = application. startuppath + "\" + m_files [I];

If (file. exists (destfilename ))

{

File. Delete (destfilename );

}

File. Copy (sourcefilename, destfilename, false );

}

// The upgrade is complete. Start the main program.

String strexe = application. startuppath + "\ test attendance system .exe ";

 

If (file. exists (strexe ))

{

System. Diagnostics. process. Start (strexe );

}

 

// Close the Upgrade Program

Application. Exit ();

}

 

# Auxiliary region method to ensure thread security

 

/// <Summary>

/// Display information on the label

/// </Summary>

Private void updateui (Object sender, eventargs E)

{

Label LBL = (Label) sender;

LBL. forecolor = systemcolors. desktop;

Lbllocalver. Text = m_strlocalver;

Lblserverver. Text = m_strserverver;

}

 

/// <Summary>

/// Display dialog box

/// </Summary>

Private void onshowmessage (Object sender, eventargs E)

{

MessageBox. Show (this, Sender. tostring (), this. Text,

Messageboxbuttons. OK, messageboxicon. information );

Thread. Sleep (100 );

This. Close ();

}

/// <Summary>

/// Display the progress bar

/// </Summary>

Private void invokeprgbar ()

{

If (maid. value <100)

{

Prgbar. value = 100 * (INT) m_downedfilesize/m_allfilesize;

Prgbar. Update ();

System. Diagnostics. Debug. writeline ("prgbar. Value: {0}" + prgbar. value );

}

}

/// <Summary>

/// Calculate the percentage of downloaded files

/// </Summary>

Private void subprgbar ()

{

M_downedfilesize + = m_downingfilesize;

System. Diagnostics. Debug. writeline ("allfilesize" + m_allfilesize );

System. Diagnostics. Debug. writeline ("downingfilesize" + m_downingfilesize );

Do

{

Thread. Sleep (100 );

Methodinvoker MI = new methodinvoker (this. invokeprgbar );

This. begininvoke (MI );

} While (m_allfilesize <= m_downedfilesize );

}

# Endregion

 

/// <Summary>

/// Close the thread when closing the form

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "E"> </param>

Private void frmautoupdate_formclosing (Object sender, formclosingeventargs E)

{

Try

{

Downthread. Abort ();

}

Catch

{

;

}

}

}

}

:

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.