C # automatically update the local program,
About automatic system updates. Recently, it is necessary to overwrite the latest system file in the background of the java client to the local client, which is automatically updated in short.
The local system will obtain the version number of the current system to request the interface data of the background java. The base64 byte stream converted from the backend compressed package is returned to me.
The client needs to update the local program to get the new version.
if (UpdateSystem(Path.Combine(Application.StartupPath, "Version.txt"), Path.Combine(Application.StartupPath, "u.zip"))) { Application.Exit(); }
/// <Summary> /// read local version request updates /// </summary> /// <param name = "document"> Read File Information </param> /// <param name = "zipPath"> return the local path of the zip package </param> /// <returns> </returns> private bool UpdateSystem (string document, string zipPath) {try {Dictionary <string, string> postDic = new Dictionary <string, string> (); // obtain the version number in the File if (File. exists (document) {postDic. add ("version", File. readAllText (document ). trim ();} else {PostDic. add ("version", "0");} string postJson = JsonConvert. serializeObject (postDic); string url = GetAppSettingValue ("serverUrl") + "parkClient/parkClientUpdate"; // return json data JObject obj = (JObject) JsonConvert. deserializeObject (PostData (postJson, url); string newVersion = obj ["version"]. toString (); if (! String. isNullOrWhiteSpace (newVersion) {byte [] bytesFile = Convert. fromBase64String (obj ["byteArray"]. toString (); if (obj ["clientMD5"]. toString () = BitConverter. toString (new System. security. cryptography. MD5CryptoServiceProvider (). computeHash (bytesFile )). replace ("-", "") {ZipCoverage (bytesFile, zipPath); File. writeAllText (document, newVersion) ;}return true;} catch (Exception ex) {MessageB Ox. show (ex. message); return false ;}} /// <summary> /// decompress the zip package to overwrite the update. /// </summary> /// <param name = "bytes"> Accept the byte information of the update package </ param> // <param name = "zpath"> overwrite path </param> private void ZipCoverage (byte [] bytes, string zpath) {File. writeAllBytes (zpath, bytes); using (ZipArchive archive = ZipFile. openRead (zpath) {string file = null; foreach (ZipArchiveEntry entry in archive. 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> /// obtain the configuration content in the deleettings section of the configuration file /// </summary> /// <param name = "deleettingkey"> </param> /// <param name = "message"> </param> /// <returns> </returns> private string GetAppSettingValue (string appSettingKey) {ExeConfigurationFileMap map = new ExeConfigurationFileMap {ExeConfigFilename = @ "TDH.Parking.Client.exe. config "}; return ConfigurationManager. openMappedExeConfiguration (map, ConfigurationUserLevel. none ). appSettings. settings [deleettingkey]. value ;}
byte[] bytesFile = Convert.FromBase64String(obj["byteArray"].ToString());
Here is the byte stream.
This method can solve the problem where multiple projects in the same solution can read the App. config file under the same project.
Note: There is a referenced class library, which is used to operate the compressed package.
To put it down, the first step is to save the byte stream of the compressed package to the local storage. The second step is to read the file of the compressed package cyclically and replace the local file to update the version of the local system.
Whether simple or complex, you must step forward.