本文介绍了db.mdf正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在按代码显示如下:

private void DatabaseIntegrityCheck()
{
    try
    {
        string m_checksum;
        using (FileStream stream = File.OpenRead(@"C:\~\db.mdf"))
        {
            SHA256Managed sha = new SHA256Managed();
            byte[] checksum = sha.ComputeHash(stream);
            m_checksum = BitConverter.ToString(checksum).Replace("-", String.Empty);
        }

        Console.WriteLine(m_checksum);

    }
    catch (Exception ex)
    {
        Console.WriteLine("unable to retrieve checksum");

    }

}

当我在代码中设置一个断点以查看异常是什么时,我得到一个IOException:

When I set a breakpoint in my code to see what the exception is, I get a IOException that says:

该进程无法访问文件'db.mdf',因为它正在被另一个进程使用.

The process cannot access the file 'db.mdf' because it is being used by another process.

我运行校验和的方式是在我的窗口中有一个按钮,当我单击它时,上面的方法被调用以执行操作.我想检查一下是否首先起作用,所以我只用Console.WriteLine校验和哈希值,但是抛出了上面的异常.

The way I am running the checksum is there's a button in my window and when I click it, the method above is being called to perform the action. I want to check and see if it works first, so I just Console.WriteLine the checksum hash but the exception above is being thrown.

我该怎么做才能解决此问题?

What can I do to fix this?

推荐答案

尝试以下代码:

        try
        {
            string mChecksum;
            using (FileStream stream = File.OpenRead(@"E:\draft.pdf"))
            {
                var sha = new SHA256Managed();
                var cs = new CryptoStream(stream, sha, CryptoStreamMode.Read);
                cs.FlushFinalBlock();
                byte[] hash = sha.Hash;

                mChecksum = BitConverter.ToString(hash).Replace("-", String.Empty);
            }

            Console.WriteLine(mChecksum);

        }
        catch (Exception ex)
        {
            Console.WriteLine("unable to retrieve checksum");

        }

查看有关的详细信息CryptoStream

这篇关于db.mdf正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:13