使用csharp编写一段对文件夹下所有文件生成md5值的代码
时间: 2023-02-07 18:13:26 浏览: 84
你好,下面是一段用 C# 编写的生成文件夹下所有文件的 MD5 值的代码:
```
using System;
using System.IO;
using System.Security.Cryptography;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string directoryPath = "C:\\Example\\Folder"; // 要遍历的文件夹的路径
// 遍历文件夹下的所有文件
foreach (string filePath in Directory.GetFiles(directoryPath))
{
// 读取文件内容
byte[] fileContent = File.ReadAllBytes(filePath);
// 计算文件的 MD5 值
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(fileContent);
string hashString = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
Console.WriteLine("文件:" + filePath + " 的 MD5 值为:" + hashString);
}
}
}
}
}
```
希望这段代码能帮到你。