要做一個程序,檢測目錄下指定檔案的md5是否我指定的md5 , 已經做出檢測指定檔案的md5功能 , 但如何對比檢測出來的md5和我指定的string 呢? 我用了String.Compare 對比兩個string是否一致 , 但VS報錯了 , 可能是語法有錯 =__= ,請求大大幫幫忙指點指點。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace checkmd5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var tragetFile = new System.IO.FileStream(@"Minecraft.jar", System.IO.FileMode.Open);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashbytes = md5.ComputeHash(tragetFile);
tragetFile.Close();
System.Text.StringBuilder checkmd5 = new System.Text.StringBuilder();
for (int i = 0; i < hashbytes.Length; i++)
{
checkmd5.Append(hashbytes[i].ToString("x2"));
}
string MyString = checkmd5.ToString();
string Minecraftjar = "4eca7879ff514cdd79c290701443eedd";
MessageBox.Show(String.Compare(MyString, Minecraftjar ));
}
}
}
<pre class="c" name="code">
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace checkmd5 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var tragetFile = new System.IO.FileStream(@"Minecraft.jar", System.IO.FileMode.Open);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashbytes = md5.ComputeHash(tragetFile);
tragetFile.Close();
string MyString = System.Convert.ToBase64String(hashbytes, 0,hashbytes.Length);
string Minecraftjar = "4eca7879ff514cdd79c290701443eedd";
MessageBox.Show(String.Compare(MyString, Minecraftjar ));
}
}
}
除錯的時侯
可以把 MyString, Minecraftjar 都 Console.writeln 出來
才知道兩者到底一不一樣