The deployment of winform application systems has always been an annoyance. In section 2.0, we can use clickonce to deploy the system, but here we will talk about using file operations to update the system.
To facilitate user jobs, the actual system is packaged into a DLL, and an automatically updated form is designed as the portal for user startup. After the user starts the service, the system automatically detects the server and client system file versions and updates all required files to the latest version. Then the system portal page is automatically started.
- Using system;
- Using system. Collections. Generic;
- Using system. componentmodel;
- Using system. Data;
- Using system. drawing;
- Using system. text;
- Using system. Windows. forms;
- Using system. reflection;
- Using system. Threading;
- Using system. net;
- Using system. IO;
- Namespace autoupdateandcallformdll
- {
- /// <Summary>
- /// 1. Automatic update is enabled at startup.
- /// 2. You can package different modules into a DLL and use the reflection mechanism to call the form (class) in the DLL during runtime ).
- ///
- /// Asynchronous method is used to accurately display the updated image and call the DLL after the update.
- /// </Summary>
- Public partial class form1: Form
- {
- Public form1 ()
- {
- Initializecomponent ();
- Backgroundworker1.runworkerasync (); // asynchronously update a job
- }
- Private void callmainform ()
- {
- // Dynamic call form using assembly
- Assembly = assembly. loadfrom (@ "D:/test/SD. dll ");
- Form FRM = assembly. createinstance ("SD. frmmain") as form;
- FRM. Show ();
- FRM. Activate ();
- This. Visible = false;
- }
- Private void updatedll ()
- {
- String filename = @ "D:/test" + "//" + "DD. dll"; // local path
- String url = @ "http: // webtest/serverdownload" + "/" + "A. dll"; // server path
- Try
- {
- // Update through http to avoid restrictions on network and folder permissions.
- Webrequest mywebrequest = webrequest. Create (URL );
- Webresponse mywebresponse = mywebrequest. getresponse ();
- Stream receivestream = mywebresponse. getresponsestream ();
- // Delete historical files first
- File. Delete (filename );
- Filestream FS = new filestream (filename, filemode. createnew );
- Try
- {
- List <byte> lstbyte = new list <byte> ();
- While (true)
- {
- // Upload one byte and one byte
- Int I = receivestream. readbyte ();
- If (I =-1)
- {
- Break;
- }
- FS. writebyte (convert. tobyte (I ));
- }
- }
- Finally
- {
- If (FS! = NULL)
- FS. Close ();
- Mywebresponse. Close ();
- }
- }
- Catch (exception ex)
- {
- Throw ex;
- }
- Finally
- {
- Webrequest. defaultwebproxy = NULL;
- }
- }
- Private void backgroundworker=dowork (Object sender, doworkeventargs E)
- {
- Thread. Sleep (3000); // This method can be used to clearly view updates
- Updatedll (); // asynchronously update a file
- }
- Private void backgroundworkerappsrunworkercompleted (Object sender, runworkercompletedeventargs E)
- {
- Callmainform (); // call this method after the asynchronous job is completed
- }
- }
- }
The example is a simple implementation. In a job, you can perform directory operations and compare the version of each file (or the last modification time). If a new version appears, it is updated.
Key points:
1. version Comparison
2. asynchronous job (backgroundworker component)
3. download files through HTTP (webrequest class)
4. reflection mechanism for dynamic form calling
5. Create a virtual directory in IIS for HTTP access to the server folder.