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.IO;
9 using System.Diagnostics;
10
11 namespace UpdateDllApplication1
12 {
13 public partial class Form1 : Form
14 {
15 // 思路:
16 // 先建立一个表A 保存 所有DLL 位置 和 DLL 修改时间
17 // 然后建立一个表B 保存 有最大修改时间的 DLL 位置 和 DLL 修改时间
18 // 此两个表建立完成后 从表B 每个DLL 依次在 表A 找修改时间小于表B 同名DLL 的
19 // 然后 复制 并写入到ListBox1!
20
21 private bool Stop = false;
22
23 private struct Filees
24 {
25 /// <summary>
26 /// 路径+文件名
27 /// </summary>
28 public string FullName;
29 /// <summary>
30 /// 文件名
31 /// </summary>
32 public string FileName;
33 /// <summary>
34 /// 修改时间
35 /// </summary>
36 public DateTime ModifyTime;
37 }
38
39 public Form1()
40 {
41 InitializeComponent();
42 }
43
44 private void button1_Click(object sender, EventArgs e)
45 {
46 FolderBrowserDialog fd = new FolderBrowserDialog();
47 fd.Description = "选择一个文件夹,此程序将更新此文件夹中所有DLL 至最新版本";
48 fd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //我的文档
49 fd.ShowNewFolderButton = false;
50 if (fd.ShowDialog(this) == DialogResult.OK)
51 {
52 textBox1.Text = fd.SelectedPath;
53 }
54 }
55
56 private void button2_Click(object sender, EventArgs e)
57 {
58 // 扫描文件夹中 DLL 位置 和 修改时间 并保存到两个表
59 // 一个表里保存的全部
60 // 另一表里保存的最新修改时间的 DLL 位置和时间
61
62 // 下面代码有误 只能查当前文件夹, 正确写法需要递归
63 List<Filees> A = new List<Filees>();
64 List<Filees> B = new List<Filees>();
65 // 切莫忘了清空 如果不需要即时清空 也要添加右键菜单的清空操作.
66 listBox1.Items.Clear();
67 string path = textBox1.Text.Trim();
68 if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
69 {
70 SearchFiles(path, A, B);
71 }
72 // 开始修改
73 bool checkVer = checkBox1.Checked;
74 if (MessageBox.Show(this, "这样会修改很多文件,开始前需要确认", "提示") == DialogResult.OK)
75 {
76 foreach (Filees item in A.ToArray())
77 {
78 if (Stop)
79 return;
80 foreach (Filees items in B.ToArray())
81 {
82 if (Stop)
83 return;
84 if (item.FileName == items.FileName)
85 {
86 if (!checkVer || (checkVer && ver(item.FullName) == ver(items.FullName))) //检查版本号
87 {
88 if (item.ModifyTime < items.ModifyTime)
89 {
90 listBox1.Items.Add(item.FullName);
91 try
92 {
93 File.Copy(items.FullName, item.FullName, true);
94 }
95 catch (Exception ex)
96 {
97 listBox1.Items.Add(" 出错!!!!!!\t" + ex.Message);
98 }
99 }
100 }
101 }
102 }
103 }
104 MessageBox.Show(this, "已完成!!!", "提示");
105 }
106 }
107
108 private string ver(string path)
109 {
110 try
111 {
112 return System.Reflection.Assembly.LoadFrom(path).GetName().Version.ToString();
113 }
114 catch (Exception ex)
115 {
116 listBox1.Items.Add(" 出错!!!!!!\t\t" + ex.Message);
117 return null;
118 }
119 }
120
121 private void button3_Click(object sender, EventArgs e)
122 {
123 Stop = true;
124 }
125
126 private void SearchFiles(string path, List<Filees> A, List<Filees> B)
127 {
128 foreach (string item in Directory.GetFiles(path))
129 {
130 if (item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !item.Contains("Designer"))
131 {
132 Filees temp = new Filees();
133 temp.FullName = item;
134 temp.FileName = GetFileName(item);
135 temp.ModifyTime = File.GetLastWriteTime(item);
136 A.Add(temp);
137 //WriteToTxt("D:\\1.txt", temp.FileName, temp.ModifyTime.ToString("yyyy-MM-dd HH:mm"));
138 //更新B
139 bool b = false;
140 foreach (Filees items in B.ToArray())
141 {
142 if (items.FileName == temp.FileName)
143 {
144 b = true;
145 if (items.ModifyTime < temp.ModifyTime)
146 {
147 B.Remove(items);
148 B.Add(temp);
149 }
150 }
151 }
152 if (!b)
153 B.Add(temp);
154 }
155 }
156 foreach (string item in Directory.GetDirectories(path))
157 {
158 SearchFiles(item, A, B);
159 }
160 }
161
162 void WriteToTxt(string path, string text, string time)
163 {
164 using (StreamWriter sw = new StreamWriter(path, true))
165 {
166 sw.WriteLine(text + "\t" + time);
167 sw.Close();
168 }
169 }
170
171 /// <summary>
172 /// 获取指定文件后缀名
173 /// </summary>
174 /// <param name="str">文件</param>
175 /// <returns>待返回的路径名</returns>
176 private string GetSuffix(string str)
177 {
178 string temp = str.Trim();
179 if (string.IsNullOrEmpty(temp))
180 return "";
181 temp = GetFileName(temp);
182 int i = temp.LastIndexOf('.');
183 if (i > -1)
184 return temp.Substring(i + 1);
185 return "";
186 }
187
188 /// <summary>
189 /// 获取指定路径文件名
190 /// </summary>
191 /// <param name="str">路径</param>
192 /// <returns>待返回的文件名</returns>
193 private string GetFileName(string str)
194 {
195 if (string.IsNullOrEmpty(str))
196 return "";
197 int i = str.LastIndexOf('\\');
198 return str.Substring(i + 1);
199 }
200
201 /// <summary>
202 /// 获取字符串中路径
203 /// </summary>
204 /// <param name="str">字符串</param>
205 /// <returns>待返回的路径</returns>
206 private string GetFullPath(string str)
207 {
208 if (string.IsNullOrEmpty(str))
209 return "";
210 int i = str.LastIndexOf('\\');
211 return str.Substring(0, i);
212 }
213
214 private void clearToolStripMenuItem_Click(object sender, EventArgs e)
215 {
216 listBox1.Items.Clear();
217 }
218
219 private void listBox1_MouseDown(object sender, MouseEventArgs e)
220 {
221 // 此代码有错,当出现滚动条时 就会选错
222 if (e.Button == MouseButtons.Right)
223 {
224 int i = e.Y / listBox1.ItemHeight;
225 listBox1.SelectedIndex = i < listBox1.Items.Count ? i : listBox1.Items.Count - 1;
226 }
227 }
228
229 private void copyToolStripMenuItem_Click(object sender, EventArgs e)
230 {
231 Clipboard.Clear();
232 Clipboard.SetText("第 " + (listBox1.SelectedIndex + 1).ToString() + " 行\t" + listBox1.SelectedItem.ToString());
233 }
234
235 private void toTxtToolStripMenuItem_Click(object sender, EventArgs e)
236 {
237 SaveFileDialog fd = new SaveFileDialog();
238 fd.FileName = "D:\\1.txt";
239 fd.Filter = "文本文件|*.txt|全部文件|*.*";
240 if (fd.ShowDialog() == DialogResult.OK)
241 {
242 using (StreamWriter sw = new StreamWriter(fd.FileName, false))
243 {
244 foreach (string item in listBox1.Items)
245 {
246 sw.WriteLine(item);
247 }
248 sw.Close();
249 }
250 }
251 }
252
253 private void listBox1_DoubleClick(object sender, EventArgs e)
254 {
255 try
256 {
257 Process.Start("Explorer.exe", GetFullPath(listBox1.SelectedItem.ToString()));
258 }
259 catch
260 {
261 MessageBox.Show(this, "这个路径不合法!\r\n\r\n无法打开所在文件夹!", "提示");
262 }
263 }
264
265 private void button4_Click(object sender, EventArgs e)
266 {
267 // 扫描文件夹中 DLL 位置 和 修改时间 并保存到两个表
268 // 一个表里保存的全部
269 // 另一表里保存的最新修改时间的 DLL 位置和时间
270
271 //私有变量 目的是减少内存占用 或者说减少出错。
272 List<Filees> A = new List<Filees>();
273 List<Filees> B = new List<Filees>();
274 // 切莫忘了清空 如果不需要即时清空 也要添加右键菜单的清空操作.
275 listBox1.Items.Clear();
276 string path = textBox1.Text.Trim();
277 if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
278 {
279 SearchFiles(path, A, B);
280 }
281 bool checkVer = checkBox1.Checked;
282 foreach (Filees item in A.ToArray())
283 {
284 foreach (Filees items in B.ToArray())
285 {
286 if (item.FileName == items.FileName)
287 {
288 if (!checkVer || (checkVer && ver(item.FullName) == ver(items.FullName))) //检查版本号
289 {
290 if (File.GetLastWriteTime(item.FullName) < File.GetLastWriteTime(items.FullName))
291 {
292 listBox1.Items.Add(item.FullName + "\t修改时间:" + item.ModifyTime + "\t最新时间:" + items.ModifyTime);
293 }
294 }
295 }
296 }
297 }
298 if (listBox1.Items.Count == 0)
299 listBox1.Items.Add("已扫描完成,Dll无需更新!");
300 }
301 }
302 }