テキスト ファイルから 5 つの MD 5 ハッシュのグループを取得するウイルス対策プログラムを作成しようとしています。ユーザーが参照を使用してフォルダーを選択し、スキャンをクリックした後、プログラムはすべてのファイルを反復処理して、各ファイルのハッシュを実行します。バックグラウンド ワーカーと比較し、結果がウイルス検出の場合、結果はリスト ボックスに送られます。
現在、コードは (FileNotFoundException) をキャッチしており、FilePath を表示する msgbox は「Path1」「Path2」「Path3」を生成しています。パスが正しく供給されるようにコードを修正する方法を誰か説明できますか? 「Path1」「Path2」「Path3」ectがどこから来ているのかよくわかりませんか?
以下のコード:
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;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace TestAntivirus
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int noofvirus = 0;
List<string> completehashes = new List<string>();
private void Browse_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
label1.Text = folderBrowserDialog1.SelectedPath;
noofvirus = 0;
label2.Text = "Viruses:" + noofvirus.ToString();
progressBar1.Value = 0;
listBox1.Items.Clear();
}
private void BDone_Click(object sender, EventArgs e)
{
this.Close();
}
private void BScan_Click(object sender, EventArgs e)
{
string[] scanned = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
int numofscanned = scanned.Count();
progressBar1.Maximum = scanned.Length;
foreach (string item in scanned)
{
for (int i = 0; i < (numofscanned); i++)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler (backgroundWorker1_DoWork);
worker.RunWorkerAsync(i);
}
}
foreach (string hashes in completehashes)
{
try
{
StreamReader stream = new StreamReader(hashes);
string read = stream.ReadToEnd();
var lineCount = File.ReadLines(@"C:\Users\Neil Bagley\Desktop\ProjectWork\VirusHashes\Test5.txt").Count();
var virus = File.ReadAllLines(@"C:\Users\Neil Bagley\Desktop\ProjectWork\VirusHashes\Test5.txt");
foreach (string st in virus)
{
if (Regex.IsMatch(read, st))
{
MessageBox.Show("Virus Detected");
noofvirus += 1;
label2.Text = "Viruses: " + noofvirus.ToString();
listBox1.Items.Add(hashes);
}
progressBar1.Increment(1);
}
}
catch
{
string read = hashes;
var virus = File.ReadAllLines(@"C:\Users\Neil Bagley\Desktop\ProjectWork\VirusHashes\Test5.txt");
foreach (string st in virus)
{
if (Regex.IsMatch(read, st))
{
MessageBox.Show("Virus Detected");
noofvirus += 1;
label2.Text = "Viruses:" + noofvirus.ToString();
listBox1.Items.Add(hashes);
}
}
}
}
}
private static String MakeHashString(byte[] hashbytes)
{
StringBuilder hash = new StringBuilder(32);
foreach (byte b in hashbytes)
{
hash.Append(b.ToString("X2").ToLower());
}
return hash.ToString();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string filePath = e.Argument.ToString();
byte[] buffer;
int bytesRead;
long size;
long totalBytesRead = 0;
try
{
using (Stream file = File.OpenRead(filePath))
{
size = file.Length;
using (HashAlgorithm hasher = MD5.Create())
{
do
{
buffer = new byte[4096];
bytesRead = file.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
backgroundWorker1.ReportProgress((int)((double)totalBytesRead / size * 100));
} while (bytesRead != 0);
hasher.TransformFinalBlock(buffer, 0, 0);
e.Result = MakeHashString(hasher.Hash);
}
}
}
catch (FileNotFoundException)
{
MessageBox.Show("File not found in the specified path" + filePath);
}
catch (IOException)
{
MessageBox.Show("IOException");
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
completehashes.Add(e.Result.ToString());
progressBar1.Value = 0;
}
}
}
以前に与えられたアドバイスの後、私はプロジェクトの作り直しを開始しようとしましたが、「foreach (スキャンされた文字列スキャン)」で期待したすべてではなく、リストボックスに 1 つのハッシュのみが追加される理由がわかりません。誰か説明できますか? 新しい試み:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace AntiVirus
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int noofvirus = 0;
private string[] scanned;
private void BBrowse_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
label1.Text = folderBrowserDialog1.SelectedPath;
noofvirus = 0;
label2.Text = "Viruses:" + noofvirus.ToString();
progressBar1.Value = 0;
listBox1.Items.Clear();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
scanned = Directory.GetFiles(e.Argument.ToString(), "*.*", SearchOption.AllDirectories);
foreach (string scan in scanned)
{
byte[] buffer;
int bytesRead;
long size;
long totalBytesRead = 0;
using (Stream file = File.OpenRead(scan))
{
size = file.Length;
using (HashAlgorithm hasher = MD5.Create())
{
do
{
buffer = new byte[4096];
bytesRead = file.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
backgroundWorker1.ReportProgress((int)((double)totalBytesRead / size * 100));
} while (bytesRead != 0);
hasher.TransformFinalBlock(buffer, 0, 0);
e.Result = MakeHashString(hasher.Hash);
}
}
}
}
private static String MakeHashString(byte[] hashbytes)
{
StringBuilder hash = new StringBuilder(32);
foreach (byte b in hashbytes)
{
hash.Append(b.ToString("X2").ToLower());
}
return hash.ToString();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
listBox1.Items.Add(e.Result.ToString());
progressBar1.Value = 0;
}
private void BScan_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(folderBrowserDialog1.SelectedPath);
}
private void BDone_Click(object sender, EventArgs e)
{
this.Close();
}
}
}