Example introduction file download, registry operation, etc. Basic skills! (Original)

xiaoxiao2021-03-06  119

I'm learning. Net time is not very long, write a little Dongdong, everyone sharing, wrong, please point out. Obtained into the topic.

??? This applet is what I did for a friend of a friend. It is not very high, it is some C # simple applications.

In the process, I specially appreciate the convenience and speed of developing Windows applications with .NET.

The function of the program is to complete the upgrade of the application. The idea is to put the file that needs to be upgraded into a compressed package, and the upgrade program is passed.

??? Compressed package download to the local, automatically unpack, cover the file to the specified directory of the application. In the process, it also mentions the current two questions.

The question is to turn off the main program of the app when the update is updated, and the upgrade will be started again. Let's see the entire implementation:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Threading; using System.Diagnostics; using Microsoft.Win32; Using system.Runtime.InteropServices;

[DLLIMPORT ("unzip.dll")] ?? public static extern int myzip_extractfile (string zipfile, string pathname); // This declaration must be placed in the form class

Private void form1_load (object sender, system.eventargs e) ?? {??? thread ds_test = new thread (new threadstart (test)); // established a thread ??? ds_test.isbackground = true; ??? ds_test .Start ();??

???? / * The entire program is also from here. In the Form1_Load event I thrown a thread to perform test network connectivity, because I write too much code in the Form1_Load event, I am afraid that I will affect the speed of the form startup. * / ??} ?? // Test if the network is connected to the function ?? private void test () ?? {?????? label1.forecolor = system.drawing.color.red; // Label display "is detection Network configuration "??? Try ??? // uses try and catch structures to get the exception of ServerAddress.getResponse (), give the corresponding prompt ??? {???? WebRequest ServerAddress = WebRequest.create (" http: //www.zz.ha.cn/" "etc?? erSERVERADDRESS.GETRESPONSE (); // Return to the Internet Requests Response ???? Label6.Text =" Server is connected "; ??

??}

Catch ??? {???? messagebox.show ("Network connection failed, please make sure your machine has been connected to the Internet", "Dinghao Intelligent Upgrade Procedure", MessageBoxButton.ok, MessageBoxicon.Warning; ???? Application.exit (); ??????} ???? label2.forecolor = system.drawing.color.red; // Label display "Close the current open application" ???? killprocess (); ?? // Call the function of the closing process to close the application

??????????1read DS_DOWNLISTFILE = New Thread (New ThreadStart (DownListFile)); // Throw new thread to download the upgrade package ???? DS_DOWNLISTFILE.IsBackground = true; ???? DS_DOWNLISTFILE.START (); ?? ?} ???} // Download the upgrade file package ?? private void DownloadListFile () ?? {??????? label3.forecolor = system.drawing.color.red; // Download Upgrade file ???? ?????????? String pathname = readreg (); // Get the location of the installer ?????? string zipfile = pathname "// LTXUPGRADE.ZIP";? // Specify download to the local Path ?????????? String MyStringWebResource = NULL; ?????? mystringWebResource = "http://www.ds-software.net/soft/ganzhou/ltXUPGRADE.ZIP";? // Specify Online file address ?????? WebClient mywebclient = new WebClient (); ???????? {???????? mywebclient.downloadfile (MyStringWebResource, zipfile); ?? // The specified address download file to the local ?????} ???? catch ???? {????? messagebox.show ("Download the upgrade package is not successful! Please restart this upgrade program!", "Ding Ding"晟 Intelligent Upgrade Procedure ", MessageBoxButtons.ok, MessageBoxicon.warning; ????? Application.exit (); ?????????} ??? label4.color = system.drawing.color.red ; ??? unzipfile (zipfile, pathname); // Because the upgrade file is solved by the decompression function in the compressed package ??} ??

// override the file in the upgrade package to the program directory

// This uses unzip.dll here, which can complete compression and decompression functions. Use standard compression mode. ?? private void unzipfile (string zipfile, string pathname) ?? {??? int m = myzip_extractfileAll (zipfile, pathname); ??? f (m <0) ??? {???? messagebox.show (" Update file failed! " .Show ("There is an error in the upgrade package, please contact Dinghao Technology Technical Service! Service Phone 0371-7438877", "Dinghao Software Intelligent Upgrade Procedure", MessageBoxButtons.ok, MessageBoxicon.Warning); ???? Application.exit (); ???} ??? MessageBox.show ("Congratulations! The software has been updated to become the latest version, thank you! ); ??? STARTGZ_APP ();? // Call the function of the launch application ??? Application.exit (); ?????} ?? // Kill the salary software process ?? Private void killprocess () ?? {??? system.diagnostics.process myproc = new system.diagnostics.process (); ???? // get all open processes ??? Try ??? {???? foreach (Process thisproc in process) .GetProcesSbyName ("LTX"))? // Applications LTX in the process list, traverse the entire process list, find the process of this name, ???? {??????????? thisproc. Kill (); ?? // Call the kill () method to kill the process ???????????????????} ???? MessageBox. Show ("Try to close the current Ding Wei salary software failed! Please close first, then restart this upgrade program!", "Dinghao Smart Upgrade Procedure"); ???? Application.exit (); ??????? } ??} // Start the application's function???

Private void startgz_app () ?? {??? system.diagnostics.processStartInfo? info? =? new? system.diagnostics.processStartInfo (); ?????? info.FileName? =? "ltx.exe";

?? String keyvalue = readreg (); ??? info.workingdirectory? = Keyvalue; ??? system.diagnostics.process? Proc?; ???? proc? =? System.diagnostics .Process.start (info); ???? oprogress (100); ???????} ???????? {???? messagebox.show ("If you can't find a file, please install Dinghao Personnel Salary Management System "," Dinghao Intelligent Upgrade "); ???} ??}

/ / Read the AppPath for the registry to get the installation path of the application

Private? string? Readreg ()

{

?? RegistryKey reg1 = registry.localmachine; ??? registryKey reg2 = reg1.opensubKey ("Software"); ??? registryKey reg3 = reg2.opensubkey ("Retirement Manager Management System"); // Application In the registry The name ??? String AppPath = reg3.getValue ("AppPath"). TOSTRING (); ?? // Get the value of apppath ?? Return AppPath; // Return to the path where the application is located

} // Provide judgment to prevent users from unintentionally shutting down upgrade programs in the closing event

Private void form1_closing (object sender, system.componentmodel.canceleventargs e) ?? {?????? f (MessageBox.show ("Do you want to exit Upgrade Programs", "Dinghao Intelligent Upgrade Procedure", MessageboxButtons.yesno, MessageBoxicon.question) ???? == DialogResult.yes ??? {???? Application.exit (); ???} ??? Else ??? {???? e.cancel = true; ???} ?????} ????}

It is done through the entire program here, and I hope this simple application can help it needs.

Unzip.dll on the application call. I will write its declaration method and provide the download of the file.

转载请注明原文地址:https://www.9cbs.com/read-126304.html

New Post(0)