惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
U
Unit 42
人人都是产品经理
人人都是产品经理
罗磊的独立博客
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
B
Blog RSS Feed
WordPress大学
WordPress大学
腾讯CDC
H
Help Net Security
博客园 - Franky
博客园 - 【当耐特】
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
B
Blog
Vercel News
Vercel News
博客园 - 司徒正美

博客园 - 邢帅杰

.net core使用SharpZipLib压缩zip文件并设置密码 CSRedisCore用法 Android 常用数据目录(内部 / 外部、缓存、文件) 的 获取方法对照表 安卓把assets中的文件copy到app目录中 oracle执行sql语句前清除缓存 安卓开发使用interface自定义回调函数 安装DockerDesktop并启用 oracle中decode用法 vue使用import.meta编译报错,import.meta.env报:类型“ImportMeta”上不存在属性“env”。必须配置module。 oracle游标使用详解 oracle存储过程中声明一个行变量,接收游标中的行数据。variable_name table_name%ROWTYPE oracle NVL和NVL2 oracle查询存储过程和函数中是否包含某个字符串 Android清除WebView缓存 C#获取当前日期是星期几 切换项目git地址,项目迁移到新git地址 C#线程同步、跨进程同步Mutex详解、C#只允许运行一个实例 Android Stack说明 安卓打开第三方app并传入参数 安卓如何唤醒深度睡眠的设备并执行任务 java两个日期相差秒数
C#获取文件md5码
邢帅杰 · 2025-10-21 · via 博客园 - 邢帅杰

代码

/// <summary>
/// 原文链接:https://blog.csdn.net/makenothing/article/details/39493779
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static string GetMD5HashFromFile(string fileName)
{
    try
    {
        FileStream file = new FileStream(fileName, FileMode.Open);
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(file);
        file.Close();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2"));
        }
        return sb.ToString();
    }
    catch (Exception ex)
    {
        throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
    }
}

选择文件

private void btnSelectFile1_Click(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.InitialDirectory = "C:\\Users\\jay.star\\Desktop"; // 初始目录
        openFileDialog.Title = "选择文件"; // 对话框标题
        openFileDialog.Filter = "所有文件(*.*)|*.*"; // 文件过滤器

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            // 获取文件路径
            string filePath = openFileDialog.FileName;
            var md5Str = GetMD5HashFromFile(filePath);
            txtFile1.Text = md5Str;
        }
    }
}