Winform通用自动更新程序,如何实现自动更新功能?
摘要:通用自动更新程序 主要功能: 1. 可用于 CS 程序的更新,集成到宿主主程序非常简单和配置非常简单,或不集成到主程序独立运行。 2. 支持 HTTP、FTP、WebService等多种更新下载方式,下载前判断本地文件是否存在,存在则计算
通用自动更新程序
主要功能:
1. 可用于 C/S 程序的更新,集成到宿主主程序非常简单和配置非常简单,或不集成到主程序独立运行。
2. 支持 HTTP、FTP、WebService等多种更新下载方式,下载前判断本地文件是否存在,存在则计算文件 Hash,避免重复下次。
3. 支持多级目录更新,支持空文件、文件夹的更新
4. 支持更新程序的自更新
5. 支持二种更新方式:自动更新、手动点击按钮更新
6. 有多种容错处理机制
7. 支持更新前后的脚本
如何使用:
1.将二个文件拷贝到 APP 根目录,并在 APP 的 Main 方法代码前加上如下代码:
更新大致流程:
1. 检测更新
获取服务器上的更新信息,与本地的版本信息对比
2. 下载更新
将更新的文件、文件夹下载到更新临时目录
3. 安装更新
将更新临时目录的文件、文件夹移动到主程序目录中
注意:如果发现 WagUpd.exe 自身,则其拷贝到主程序目录下且命名为 ~WagUpd.exe,自动关闭现有程序,重新启动 ~WagUpd.exe
4. 完成更新
更新前、更新后的脚本
脚本可通过文件方式推送到客户端 ScriptUpdDoBeforeScriptUpdDoEnd 文件夹中,执行成功后,转移到 Done 文件夹中。
如注册 ocx 组件的事例如下:
1 <?xml version="1.0" encoding="utf-16"?>
2 <SuperScript>
3 <ScriptInfo>
4 <Language>C#</Language>
5 <UpdateTime>2016-07-25 10:58:48 4455</UpdateTime>
6 <ScriptName />
7 <Remark />
8 </ScriptInfo>
9 <RefAssembly>
10 <Name>mscorlib.dll</Name>
11 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
12 <Type>System</Type>
13 </RefAssembly>
14 <RefAssembly>
15 <Name>System.dll</Name>
16 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
17 <Type>System</Type>
18 </RefAssembly>
19 <RefAssembly>
20 <Name>System.Data.dll</Name>
21 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
22 <Type>System</Type>
23 </RefAssembly>
24 <RefAssembly>
25 <Name>System.Xml.dll</Name>
26 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
27 <Type>System</Type>
28 </RefAssembly>
29 <RefAssembly>
30 <Name>System.Windows.Forms.dll</Name>
31 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
32 <Type>System</Type>
33 </RefAssembly>
34 <RefAssembly>
35 <Name>System.Drawing.dll</Name>
36 <Path>%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319</Path>
37 <Type>System</Type>
38 </RefAssembly>
39 <RefAssembly>
40 <Name>WagUpd.Core.dll</Name>
41 <Path>E:\_Wagwei_Coding\WagUpd\WagUpd-20160624-1806\WagUpd.Core\bin\Debug</Path>
42 <Type>Custom</Type>
43 </RefAssembly>
44 </SuperScript>
45
46 /* 脚本代码 */
47
48 #region Namesapce
49 using System;
50 using System.Xml;
51 using System.IO;
52 using System.Text;
53 using System.Data;
54 using System.Threading;
55 using System.Diagnostics;
56 using System.Reflection;
57 using System.Collections;
58 using System.Windows.Forms;
59 using System.Collections.Generic;
60
61 using WagUpd.Core;
62 #endregion
63
64
65 public class RegCom : WagIUpdDo
66 {
67 //ScriptUpdDoBefore ScriptUpdDoEnd
68 //Done
69
70 public bool Run(ref string message)
71 {
72 #region 是否为管理员
73 if(false)
74 {
75 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();//获得当前登录的Windows用户标示
76 System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
77
78 if (!principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))//判断当前登录用户是否为管理员
79 {
80 message = "需要管理员权限来执行";
81 return false;
82 }
83 }
84 #endregion
85
86 string windir = Environment.GetEnvironmentVariable("windir");
87
88 string path = Path.Combine(Application.StartupPath, "");
89
90 string file = Path.Combine(path, "VidDisplay.ocx");
91
92 Debug.WriteLine("ocx 文件: " + file);
93
94 if(!File.Exists(file))
95 {
96 message = "未找到文件: " + file;
97 return false;
98 }
99
100 ProcessStartInfo si = new ProcessStartInfo();
101 si.FileName = "Regsvr32.Exe";
102 si.Arguments = file;//"/s " + file;
103
104 //si.UseShellExecute = true;
105 //si.RedirectStandardInput = false;
106 //si.RedirectStandardOutput = true;
107 //si.CreateNoWindow = false;
108
109 Process p = new Process();
110 p.StartInfo = si;
111 p.Start();
112
113 //p.WaitForExit();
114 //p.Close();
115
116 message = "ok";
117 return true;
118 }
119
120 public static void Main()
121 {
122 RegCom rc = new RegCom();
123 bool b = false;
124 string message = "";
125 b = rc.Run(ref message);
126 MessageBox.Show(message);
127 }
128
129
130 //CMD C:\Windows\System32\regsvr32.exe C:\%\VidDisplay.ocx
131
132 // regsvr32是一个用来注册动态链接库(DLL)或ActiveX控件(OCX)文件的命令,其用法如下:
133 // regsvr32[/u] [/s] [/n] [/i[:cmdline]] dllname
134 // / u - 解除服务器注册
135 // /s - 无声;不显示消息框
136 // /i - DllInstall,给其传递一个可选 [cmdline] / u一起使用时,卸载dll
137 // / n - DllRegisterServer / i一起使用
138
139 // 如果要注册“MSCOMCTL.OCX”这个控件,在 MS-DOS提示符下输入“regsvr32 MSCOMCTL.OCX”,
140 // 回车后便后提示“ MSCOMCTL.OCX中的 DllRegisterServer成功。”。如果不让它显示提示,加上参数/s即可。
141
142
143 #region 非管理员运行
144 /*
145 [Window Title]
146 RegSvr32
147
148 [Content]
149 模块“D:\Users\Wagwei\Desktop\4444\8888\VidDisplay.ocx”已加载,但对 DllRegisterServer 的调用失败,错误代码为 0x80040201。
150
151 有关此问题的详细信息,请将该错误代码用作搜索字词进行在线搜索。
152
153
154 */
155 #endregion
156
157 #region 管理员运行
158 /*
159 [Window Title]
160 RegSvr32
161
162 [Content]
163 DllRegisterServer 在 D:\Users\Wagwei\Desktop\4444\8888\VidDisplay.ocx 已成功。
164
165 */
166 #endregion
167
168 }
View Code
界面截图如下:
进程关联了模块:
进程使用了文件:
自更新:
服务端工具
1. 执行更新
2. 查看备份
