I am trying to generate a files MD5 hash.
我正在尝试生成MD5哈希文件。
Essentially how it should work.
基本上它应该如何工作。
I press a browse button on my software to browse which file I want tos can > I select the file I want to scan > and it displays the MD5 hash to a label
我按下我的软件上的浏览按钮来浏览我想要的文件>我选择我要扫描的文件>它会将MD5哈希显示给标签
here is a visual example of what I am trying to accomplish.
这是我想要完成的一个视觉示例。
My question is, how do I grab the MD5 hash, I have never seen any code that grabs MD5 hashes from a file so I have no idea how its supposed to be done.
我的问题是,如何获取MD5哈希,我从未见过任何从文件中获取MD5哈希值的代码,因此我不知道它应该如何完成。
2 个解决方案
#1
0
Try this for windows forms and modify it for your needs:
尝试使用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();
}
}
It's a combination of Calculate MD5 checksum for a file and How to convert an MD5 hash to a string and use it as a file name
它是计算文件的MD5校验和和如何将MD5哈希转换为字符串并将其用作文件名的组合
#2
0
This is what worked in the end!
这是最终有效的!
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);
}
#1
0
Try this for windows forms and modify it for your needs:
尝试使用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();
}
}
It's a combination of Calculate MD5 checksum for a file and How to convert an MD5 hash to a string and use it as a file name
它是计算文件的MD5校验和和如何将MD5哈希转换为字符串并将其用作文件名的组合
#2
0
This is what worked in the end!
这是最终有效的!
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);
}