Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成文件MD5哈希按钮单击C.NET_C#_.net_Visual Studio_Md5 - Fatal编程技术网

C# 生成文件MD5哈希按钮单击C.NET

C# 生成文件MD5哈希按钮单击C.NET,c#,.net,visual-studio,md5,C#,.net,Visual Studio,Md5,我正在尝试生成文件MD5哈希 基本上,它应该如何工作 我按下软件上的浏览按钮来浏览我要扫描的文件,它可以>我选择我要扫描的文件>并将MD5哈希显示到标签上 这是一个我试图实现的可视化示例 我的问题是,如何获取MD5散列,我从未见过任何代码从文件中获取MD5散列,因此我不知道应该如何进行 请在windows窗体上尝试此操作,并根据您的需要进行修改: public partial class Form1 : Form { public Form1() { Initia

我正在尝试生成文件MD5哈希

基本上,它应该如何工作

我按下软件上的浏览按钮来浏览我要扫描的文件,它可以>我选择我要扫描的文件>并将MD5哈希显示到标签上

这是一个我试图实现的可视化示例

我的问题是,如何获取MD5散列,我从未见过任何代码从文件中获取MD5散列,因此我不知道应该如何进行


请在windows窗体上尝试此操作,并根据您的需要进行修改:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        openFileDialog1.FileOk += OpenFileDialog1_FileOk;
    }

    private void OpenFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
        string path = ((OpenFileDialog)sender).FileName;
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(path))
            {
                label1.Text = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "");
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //show file dialog on form load
        openFileDialog1.ShowDialog();
    }
}
这是一个组合 和

这就是最终的效果

public string MD5HashFile(string fn)
{
    byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
    return BitConverter.ToString(hash).Replace("-", "");

}

private void lblTitle_Load(object sender, EventArgs e)
{

}



private void scanButton_Click(object sender, EventArgs e)
{

    //Create a path to the textBox that holds the value of the file that is going to be scanned
    string path = txtFilePath.Text;

    //if there is something in the textbox to scan we need to make sure that its doing it.
    if (!File.Exists(path))
    {
                            // ... report problem to user.
      return;

    }
    else
    {
        MessageBox.Show("Scan Complete");
    }

    //Display the computed MD5 Hash in the path we declared earlier
    hashDisplay.Text = MD5HashFile(path);


}

你尝试过这个链接吗:是的,我看到了那个链接,说实话,它看起来很棒,但我不知道如何在我的软件中使用它。如果你知道我会怎么做,我希望得到一个解释:-你所说的解释是指有人能为我编码吗?因为这个例子再清楚不过了。您只需从OpenFileDialog获取文件路径,该对话框将从Browse file click事件中触发,一旦选择了文件,您就可以将该路径传递给ComputeHash方法。我将忽略该代码,看看我能做些什么来实现相似但不同的功能!很快就会回来!