C # automatically updates local programs

Source: Internet
Author: User

About Automatic Updates to the system. In recent days, it is necessary to overwrite the latest version of the Java backend system file with the local client, the abbreviation is automatically updated.

Local will get the current system version number to request the background Java interface data. Back to me is the Base64 byte stream that the background compression packet goes to.

The client will need to update the local program to get the new version.

if " Version.txt " " U.zip " ))             {                application.exit ();            }
       /// <summary>        ///read local version request update/// </summary>        /// <param name= "Document" >read the file information</param>        /// <param name= "Zippath" >return Zip package local path</param>        /// <returns></returns>        Private BOOLUpdateSystem (stringDocumentstringZippath) {            Try{Dictionary<string,string> postdic =Newdictionary<string,string>(); //get the version number within the file                if(file.exists (document)) {Postdic.add ("version", File.readalltext (document).                Trim ()); }                Else{Postdic.add ("version","0"); }                stringPostjson =Jsonconvert.serializeobject (postdic); stringurl = getappsettingvalue ("ServerURL") +"parkclient/parkclientupdate"; //the JSON data returnedJobject obj =(Jobject) jsonconvert.deserializeobject (postdata (Postjson, url)); stringNewVersion = obj["version"].                ToString (); if(!String.isnullorwhitespace (newversion)) {                    byte[] Bytesfile = convert.frombase64string (obj["ByteArray"].                    ToString ()); if(obj["clientMD5"]. ToString () = =bitconverter.tostring (NewSystem.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (Bytesfile)). Replace ("-","") {zipcoverage (bytesfile, Zippath);                       File.writealltext (document, newversion);                     }                }                return true; }            Catch(Exception ex) {MessageBox.Show (ex).                Message); return false; }        }        /// <summary>        ///Unzip zip package overwrite update/// </summary>        /// <param name= "bytes" >Accept byte information for update package</param>        /// <param name= "Zpath" >the path covered</param>        Private voidZipcoverage (byte[] Bytes,stringZpath)            {file.writeallbytes (Zpath, bytes); using(Ziparchive archive =Zipfile.openread (Zpath)) {                stringFile =NULL; foreach(Ziparchiveentry entryinchArchive. Entries) {if(!entry. Fullname.endswith ("/") ) {file=Path.Combine (Application.startuppath, entry.                        FullName); if(file.exists (file)) {file.delete (file); }}}} zipfile.extracttodirectory (Zpath, Application.startuppath)           ;         }        /// <summary>        ///get the configuration in the appsettings section in the configuration file/// </summary>        /// <param name= "Appsettingkey" ></param>        /// <param name= "message" ></param>        /// <returns></returns>        Private stringGetappsettingvalue (stringAppsettingkey) {Execonfigurationfilemap Map=NewExeconfigurationfilemap {execonfigfilename =@"TDH. Parking.Client.exe.config" }; returnconfigurationmanager.openmappedexeconfiguration (map, Configurationuserlevel.none). Appsettings.settings[appsettingkey].        Value; }

byte [] Bytesfile = convert.frombase64string (obj["byteArray"]. ToString ());

Here is the byte stream to get.

This method can be used to solve multiple projects in the same solution that can read the app. config file under the same project.

Note: There is a reference to the class library, which is used to manipulate the compressed package.

Next idea: The first step is actually to get the compressed packet of the byte stream to save to the local, the second step is to loop through the package of files to replace the local files, complete the local System version update.

Whether simple or complex, we need to step forward.

C # automatically updates local programs

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.