我在文本框中显示 MD5 哈希结果时遇到问题。该代码使用命令行可以正常工作。代码如下所示:
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
textBox1.Text = GetMD5HashFromFile();
}
private static string GetMD5HashFromFile()
{
FileStream file = new FileStream(@"C:\Desktop\test.txt", FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
我想计算哈希并直接在文本框中显示哈希字符串。我的编程概念是错误的还是缺少一些步骤?谢谢!