-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
173 lines (153 loc) · 5.67 KB
/
Form1.cs
File metadata and controls
173 lines (153 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DirectoryMD5Comparison
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 浏览按钮1点击事件
private void btnBrowse1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
txtDirectory1.Text = fbd.SelectedPath;
}
}
}
// 浏览按钮2点击事件
private void btnBrowse2_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
txtDirectory2.Text = fbd.SelectedPath;
}
}
}
// 计算文件MD5值
private string CalculateMD5(string filePath)
{
try
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
byte[] hashBytes = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
foreach (byte b in hashBytes)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}
}
catch (Exception ex)
{
return "计算失败: " + ex.Message;
}
}
// 比较按钮点击事件
private void btnCompare_Click(object sender, EventArgs e)
{
string dir1 = txtDirectory1.Text;
string dir2 = txtDirectory2.Text;
// 验证目录是否存在
if (!Directory.Exists(dir1))
{
MessageBox.Show("源目录不存在,请重新选择!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Directory.Exists(dir2))
{
MessageBox.Show("目标目录不存在,请重新选择!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 清空之前的结果
lstResults.Items.Clear();
// 获取两个目录中的所有文件
string[] files1 = Directory.GetFiles(dir1, "*.*", SearchOption.AllDirectories);
string[] files2 = Directory.GetFiles(dir2, "*.*", SearchOption.AllDirectories);
// 创建文件名到路径的映射
Dictionary<string, string> fileMap1 = new Dictionary<string, string>();
foreach (string file in files1)
{
string relativePath = file.Substring(dir1.Length).TrimStart('\\');
fileMap1[relativePath] = file;
}
Dictionary<string, string> fileMap2 = new Dictionary<string, string>();
foreach (string file in files2)
{
string relativePath = file.Substring(dir2.Length).TrimStart('\\');
fileMap2[relativePath] = file;
}
// 获取所有文件名
HashSet<string> allFiles = new HashSet<string>(fileMap1.Keys);
allFiles.UnionWith(fileMap2.Keys);
// 比较每个文件
int differentFiles = 0;
foreach (string fileName in allFiles)
{
string md5_1 = "文件不存在";
string md5_2 = "文件不存在";
if (fileMap1.ContainsKey(fileName))
{
md5_1 = CalculateMD5(fileMap1[fileName]);
}
if (fileMap2.ContainsKey(fileName))
{
md5_2 = CalculateMD5(fileMap2[fileName]);
}
// 添加到结果列表
ListViewItem item = new ListViewItem(fileName);
item.SubItems.Add(md5_1);
item.SubItems.Add(md5_2);
// 如果MD5不同,高亮显示
if (md5_1 != md5_2 && md5_1 != "文件不存在" && md5_2 != "文件不存在")
{
item.BackColor = Color.Yellow;
differentFiles++;
}
// 如果文件只在一个目录中存在,显示为红色
else if (md5_1 == "文件不存在" || md5_2 == "文件不存在")
{
item.BackColor = Color.LightPink;
differentFiles++;
}
lstResults.Items.Add(item);
}
// 显示比较结果统计
if (differentFiles == 0)
{
MessageBox.Show("两个目录中的文件MD5值完全相同!", "比较结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"发现 {differentFiles} 个不同的文件!", "比较结果", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// 清空按钮点击事件
private void btnClear_Click(object sender, EventArgs e)
{
txtDirectory1.Clear();
txtDirectory2.Clear();
lstResults.Items.Clear();
}
}
}