Winform automatically updates and dynamically calls form implementation

Source: Internet
Author: User

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.

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. componentmodel;
  4. Using system. Data;
  5. Using system. drawing;
  6. Using system. text;
  7. Using system. Windows. forms;
  8. Using system. reflection;
  9. Using system. Threading;
  10. Using system. net;
  11. Using system. IO;
  12. Namespace autoupdateandcallformdll
  13. {
  14. /// <Summary>
  15. /// 1. Automatic update is enabled at startup.
  16. /// 2. You can package different modules into a DLL and use the reflection mechanism to call the form (class) in the DLL during runtime ).
  17. ///
  18. /// Asynchronous method is used to accurately display the updated image and call the DLL after the update.
  19. /// </Summary>
  20. Public partial class form1: Form
  21. {
  22. Public form1 ()
  23. {
  24. Initializecomponent ();
  25. Backgroundworker1.runworkerasync (); // asynchronously update a job
  26. }
  27. Private void callmainform ()
  28. {
  29. // Dynamic call form using assembly
  30. Assembly = assembly. loadfrom (@ "D:/test/SD. dll ");
  31. Form FRM = assembly. createinstance ("SD. frmmain") as form;
  32. FRM. Show ();
  33. FRM. Activate ();
  34. This. Visible = false;
  35. }
  36. Private void updatedll ()
  37. {
  38. String filename = @ "D:/test" + "//" + "DD. dll"; // local path
  39. String url = @ "http: // webtest/serverdownload" + "/" + "A. dll"; // server path
  40. Try
  41. {
  42. // Update through http to avoid restrictions on network and folder permissions.
  43. Webrequest mywebrequest = webrequest. Create (URL );
  44. Webresponse mywebresponse = mywebrequest. getresponse ();
  45. Stream receivestream = mywebresponse. getresponsestream ();
  46. // Delete historical files first
  47. File. Delete (filename );
  48. Filestream FS = new filestream (filename, filemode. createnew );
  49. Try
  50. {
  51. List <byte> lstbyte = new list <byte> ();
  52. While (true)
  53. {
  54. // Upload one byte and one byte
  55. Int I = receivestream. readbyte ();
  56. If (I =-1)
  57. {
  58. Break;
  59. }
  60. FS. writebyte (convert. tobyte (I ));
  61. }
  62. }
  63. Finally
  64. {
  65. If (FS! = NULL)
  66. FS. Close ();
  67. Mywebresponse. Close ();
  68. }
  69. }
  70. Catch (exception ex)
  71. {
  72. Throw ex;
  73. }
  74. Finally
  75. {
  76. Webrequest. defaultwebproxy = NULL;
  77. }
  78. }
  79. Private void backgroundworker=dowork (Object sender, doworkeventargs E)
  80. {
  81. Thread. Sleep (3000); // This method can be used to clearly view updates
  82. Updatedll (); // asynchronously update a file
  83. }
  84. Private void backgroundworkerappsrunworkercompleted (Object sender, runworkercompletedeventargs E)
  85. {
  86. Callmainform (); // call this method after the asynchronous job is completed
  87. }
  88. }
  89. }

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.

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.