-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (54 loc) · 1.8 KB
/
Program.cs
File metadata and controls
56 lines (54 loc) · 1.8 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
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace FastMD5
{
internal static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
string filePath = args[0];
string md5Hash = GetFileMD5Hash(filePath);
if (md5Hash != null)
{
Clipboard.SetText(md5Hash);
MessageBox.Show($"MD5 Hash: {md5Hash}\n已复制入剪切板中~", "MD5 Calculation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
Application.Run(new Form1());
}
}
public static string GetFileMD5Hash(string filePath)
{
try
{
using (FileStream stream = File.OpenRead(filePath))
{
MD5 md5 = MD5.Create();
byte[] hashValue = md5.ComputeHash(stream);
StringBuilder hex = new StringBuilder(hashValue.Length * 2);
foreach (byte b in hashValue)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
//MD5计算方法参考自https://www.cnblogs.com/lzhdim/p/18096142
}
catch (Exception ex)
{
MessageBox.Show($"Error computing MD5 hash for file {filePath}: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
}
}