Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 需要帮助在C中计算MD5哈希吗_C# - Fatal编程技术网

C# 需要帮助在C中计算MD5哈希吗

C# 需要帮助在C中计算MD5哈希吗,c#,C#,我目前正在尝试制作一个程序来计算文件的MD5散列, 但是,我得到以下错误: System.ArgumentNullException:'路径不能为null。 参数名称:路径' 这是到目前为止我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Security.C

我目前正在尝试制作一个程序来计算文件的MD5散列, 但是,我得到以下错误:

System.ArgumentNullException:'路径不能为null。 参数名称:路径'

这是到目前为止我的代码

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

    namespace MD5Checker
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void btnOpenFileDialog_Click(object sender, EventArgs e)
    {
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fname = openFileDialog.FileName;
            string FILENAME = "@" + fname;
            txtFile.Text = FILENAME;
        }
    }
    public string FILENAME { get; set; }

    private void btnCalculateMD5_Click(object sender, EventArgs e)
    {
        string results = CalculateMD5(FILENAME);
        richTextBox1.Text = results;
    }

    static string CalculateMD5(string filename)
    {
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(filename))
            {
                byte[] hash = md5.ComputeHash(stream);
                return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
            }
        }
    }
}

}问题在于,您没有更新类的属性,而是更新一个局部变量

private void btnOpenFileDialog_Click(object sender, EventArgs e)
{
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string fname = openFileDialog.FileName;

        // this is FILENAME
        string FILENAME = "@" + fname;
        // here you use FILENAME instead of this.FILENAME
        txtFile.Text = FILENAME;
    }
}

// this is this.FILENAME
public string FILENAME { get; set; }

private void btnCalculateMD5_Click(object sender, EventArgs e)
{
    // uses this.FILENAME
    string results = CalculateMD5(FILENAME);
    richTextBox1.Text = results;
}
可以删除该变量,以便始终使用该属性。当你这样做的时候,你可以开始遵循惯例,让它成为PascalCase,这样你就记住它是一个属性而不是一个变量。此外,无需在文件路径之前添加@:

private void btnOpenFileDialog_Click(object sender, EventArgs e)
{
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        FileName = openFileDialog.FileName;
        txtFile.Text = FileName;
    }
}

// this is this.FileName
public string FileName { get; set; }

private void btnCalculateMD5_Click(object sender, EventArgs e)
{
    // uses this.FileName
    string results = CalculateMD5(FileName);
    richTextBox1.Text = results;
}

请添加您的异常的完整stacktrace。无需在fname之前添加@。直接使用OpenFileDialogo返回的值哦,是的,但是@before-fname似乎完全没有保证,但是当我这样做时,我仍然会得到一个错误:System.NotSupportedException:“给定路径的格式不受支持”。@pika然后你应该显示CalculateMD5@pika你把@移走了吗?Windows中的路径不能以@开头,现在一切正常,谢谢您的帮助。