2016-05-08 24 views
-1

我得到这个软件,扫描我选择的文件,然后计算MD5散列。无法从方法组转换为字符串时读取文件C#.NET

我想比较计算出来的MD5哈希值与字典中的哈希列表,看看它是否匹配,以及它是否说明了某些内容。

如何以编程方式将dictionary.txt添加到我的代码中。

我尝试使用了File.OpenRead(),但我得到一个错误说

参数1:不能从转换“法团”到“字符串”

我错过了什么?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using System.Security.Cryptography; 

namespace MD5_Hash_Compare 
{ 
    public partial class lblTitle : Form 
    { 
     public lblTitle() 
     { 
      InitializeComponent(); 
     } 

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

     private Stream TestStream() 
     { 
      Stream fs = File.OpenRead(@"C:\PathToDictionary"); 
      return fs; 
     } 

     public string GetMD5(string file) 
     { 
      using (var md5 = MD5.Create()) 
      using (var stream = File.OpenRead(TestStream)) 
      return Encoding.Default.GetString(md5.ComputeHash(stream)); 
     } 

     private void lblTitle_Load(object sender, EventArgs e) 
     { 

     } 

     private void scanButton_Click(object sender, EventArgs e) 
     { 
      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"); 
      } 

      hashDisplay.Text = MD5HashFile(path); 
     } 

     private void browseButton_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       txtFilePath.Text = (ofd.FileName); 
      } 
     } 
    } 
} 

回答

3

GetMD5功能,你这样做

using (var stream = File.OpenRead(TestStream)) 

TestStream是一个函数,所以你需要添加括号来调用它。

using (var stream = File.OpenRead(TestStream())) 
+0

所以我不需要“返回Encoding.Default.GetString(md5.ComputeHash(流));”根本? –

+0

是的,你需要它......但是你需要在上面的行中的'TestStream'旁边添加'()'。 – zdimension

+0

那就是我所做的,它让我困惑,因为我得到了这个错误。 “无法从'System.IO.Stream'转换为'string'” –

相关问题