C# 快捷查看文件夹下所有文件的MD5
作者:野牛程序员:2023-12-13 16:01:03C#阅读 2671
使用以下C#代码可以快速查看文件夹下所有文件的MD5值:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main()
{
string folderPath = "文件夹路径";
if (Directory.Exists(folderPath))
{
string[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
{
string md5Hash = GetMD5HashFromFile(file);
Console.WriteLine($"文件: {file} 的MD5值是: {md5Hash}");
}
}
else
{
Console.WriteLine("指定的文件夹不存在。");
}
}
static string GetMD5HashFromFile(string fileName)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileName))
{
byte[] hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
}请将代码中的 "文件夹路径" 替换为要查看的文件夹的实际路径。此代码会遍历指定文件夹中的所有文件,并输出每个文件的MD5值。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:Python中的unittest是什么?
- 下一篇:Python中什么是遍历器?
