C# MD5 哈希器示例 [英] C# MD5 hasher example

查看:20
本文介绍了C# MD5 哈希器示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将其重新命名为示例,因为代码按预期工作.

I've retitled this to an example as the code works as expected.

我正在尝试复制文件,获取 MD5 哈希值,然后删除副本.我这样做是为了避免对另一个应用程序写入的原始文件进行进程锁定.但是,我正在锁定我复制的文件.

I am trying to copy a file, get a MD5 hash, then delete the copy. I am doing this to avoid process locks on the original file, which another app writes to. However, I am getting a lock on the file I've copied.

File.Copy(pathSrc, pathDest, true);

String md5Result;
StringBuilder sb = new StringBuilder();
MD5 md5Hasher = MD5.Create();

using (FileStream fs = File.OpenRead(pathDest))
{
    foreach(Byte b in md5Hasher.ComputeHash(fs))
        sb.Append(b.ToString("x2").ToLower());
}

md5Result = sb.ToString();

File.Delete(pathDest);

然后我在 File.Delete() 上收到进程无法访问文件"异常.

I am then getting a 'process cannot access the file' exception on File.Delete()'.

我希望使用 using 语句可以很好地关闭文件流.我还尝试单独声明文件流,删除 using,并在读取后放置 fs.Close()fs.Dispose().

I would expect that with the using statement, the filestream would be closed nicely. I have also tried declaring the filestream separately, removing using, and putting fs.Close() and fs.Dispose() after the read.

在这之后,我注释掉了实际的md5计算,代码执行,文件被删除,所以看起来它与ComputeHash(fs)有关.

After this, I commented out the actually md5 computation, and the code excutes, with the file being deleted, so it looks like it's something to do with ComputeHash(fs).

推荐答案

我把你的代码放到了一个控制台应用程序中并且没有错误地运行,得到了哈希值并且测试文件在执行结束时被删除了?我只是使用了我的测试应用程序中的 .pdb 作为文件.

I took your code put it in a console app and ran it with no errors, got the hash and the test file is deleted at the end of execution? I just used the .pdb from my test app as the file.

您运行的是哪个版本的 .NET?

What version of .NET are you running?

我把我有的代码放在这里,如果你把它放在 VS2008 .NET 3.5 sp1 的控制台应用程序中,它运行没有错误(至少对我来说).

I am putting the code that I have that works here, and if you put this in a console app in VS2008 .NET 3.5 sp1 it runs with no errors (at least for me).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace lockTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string hash = GetHash("lockTest.pdb");

            Console.WriteLine("Hash: {0}", hash);

            Console.ReadKey();
        }

        public static string GetHash(string pathSrc)
        {
            string pathDest = "copy_" + pathSrc;

            File.Copy(pathSrc, pathDest, true);

            String md5Result;
            StringBuilder sb = new StringBuilder();
            MD5 md5Hasher = MD5.Create();

            using (FileStream fs = File.OpenRead(pathDest))
            {
                foreach (Byte b in md5Hasher.ComputeHash(fs))
                    sb.Append(b.ToString("x2").ToLower());
            }

            md5Result = sb.ToString();

            File.Delete(pathDest);

            return md5Result;
        }
    }
}

这篇关于C# MD5 哈希器示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆