Treeview Scanner和MD5的问题 [英] Problem with Treeview Scanner And MD5

查看:63
本文介绍了Treeview Scanner和MD5的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望我来对地方了,这个问题已经困扰了我一段时间.我的问题很复杂,很难提出问题,但是我会尽我最大的努力陈述我的问题,同时我还会提出一些问题来澄清问题.我正在制作一个防病毒软件,它使用树视图控件通过MD5哈希进行扫描.是否可以扫描节点文件夹和子文件夹以检查MD5哈希值是否具有病毒签名?我将提供我正在处理的代码,希望这里的人可以告诉我我哪里出了问题.....
查看源代码
打印吗?

Hopefully I have come to the right place to ask this question as it has had me stuck for some time now. The complexity of my problem makes it difficult to ask but I will do my best to state my problem as best I can with a few questions as I go along to clarify the issue. I am making an anti-virus Software that scans by means of MD5 hash with the use of a tree-view control. Is it possible to scan the nodes folders and sub-folders to check MD5 hash values for virus signatures? I will supply the code I am working on in hopes that someone here can tell me where I have gone wrong.....
view source
print?

01	Dim md5hashtree As String = DirTree1.SelectedNode.Text
02	                   Try
03	                       Using scanbox As New TextBox()
04	                           Dim read As String = My.Computer.FileSystem.ReadAllText("virusSig.txt")
05	                           detected.Text = Conversions.ToString(CheckedListBox1.Items.Count)
06	                           scanbox.Text = read.ToString
07	                           Using md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
08	                               Dim f As FileStream = New FileStream(md5hashtree, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
09	                               f = New FileStream(md5hashtree, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
10	                               md5.ComputeHash(f)
11	                               Dim hash As Byte() = md5.Hash
12	                               Dim buff As StringBuilder = New StringBuilder()
13	                               Dim hashByte As Byte
14	                               For Each hashByte In hash
15	                                   buff.Append(String.Format("{0:X2}", hashByte))
16	                               Next
17	                               If scanbox.Text.Contains(buff.ToString) Then
18	                                   CheckedListBox1.Items.Add(md5hashtree)
19	                               End If
20	                           End Using
21	                       End Using
22	                   Catch ex As Exception
23	                   End Try


如果将md5hashtree替换为listview1.selecteditem,则此代码非常有用,但我需要将VirusSig.txt文件与treeview中的当前dirtyreeview.selectednode进行比较.
在我以前的程序中,此代码被放入计时器中,然后在开始"按钮中启动该计时器,但是我应如何以及在何处将该代码添加到此子程序中?

查看源代码
打印吗?


This code works great if md5hashtree is replaced with listview1.selecteditem but I need it to compare my VirusSig.txt file to the current dirtreeview.selectednode in my treeview.
In my earlier program this code is placed into a timer and then that timer is started in the start button but how and where should I add the code to this sub??

view source
print?

01	Public Sub ScanScanner(ByVal dir As String)
02	 
03	       Try
04	           With My.Computer.FileSystem
05	               Application.DoEvents()
06	               If stop_Scan = True Then
07	                   Exit Sub
08	               End If
09	               For Each file1 In .GetFiles(dir)
10	                   If stop_Scan = True Then
11	                       cmdStartScanning.Enabled = True
12	                       Exit Sub
13	                   End If
14	                   txtScanningNow.Text = file1
15	                   If gt_extension_zip(IO.Path.GetExtension(file1)) = True Then
16	                   End If
17	                   Dim fi As FileInfo = New FileInfo(file1)
18	                   Call getmyfileatr(file1)
19	                   Dim fileDetail As IO.FileInfo
20	                   fileDetail = My.Computer.FileSystem.GetFileInfo(file1)
21	                   lblSizef.Text = fileDetail.Length & "  byte(s)"
22	                   a1 = 0
23	                   a1 = (DateTime.Now.Millisecond)
24	                   a3 = 0
25	                   a3 = (DateTime.Now.Second)
26	                   Dim StartTime As New DateTime()
27	                   Dim EndTime As New DateTime()
28	                   StartTime = DateTime.Now()
29	                   txtScanningNow.Text = file1
30	                   f1 = 0
31	                   f2 = 0
32	                   a2 = 0
33	                   a2 = (DateTime.Now.Millisecond)
34	                   a4 = 0
35	                   a4 = (DateTime.Now.Second)
36	                   If a2 >= a1 Then
37	                       f1 = a2 - a1
38	                   End If
39	                   If a4 >= a1 Then
40	                       f2 = a4 - a3
41	                   End If
42	                   EndTime = DateTime.Now
43	                   Dim answer1, answer2 As Long
44	                   answer1 = 0
45	                   answer1 = EndTime.Ticks() - StartTime.Ticks()
46	                   txtTime.Text = String.Format("{0}.{1}.{2}", f2, f1, answer1)
47	                   txtTime.Refresh()
48	               Next file1
49	               ' Search child directories.
50	               For Each folder As Object In .GetDirectories(dir)
51	                   txtScanningNow.Text = folder
52	                   ScanScanner(folder)
53	               Next folder
54	           End With
55	       Catch ex As UnauthorizedAccessException
56	       End Try
57	       Exit Sub
58	   End Sub


如果有人知道如何使用TreeView扫描MD5哈希的文件夹,子文件夹和文件,那我真是不胜其烦...

非常感谢,希望很快能收到您的来信.:)


If anyone knows how to use TreeView to scan folders, subfolders and files for MD5 hash I am all ears......

Thank you very much in advance and hope to hear from someone soon.. :)

推荐答案

第一件事是我不明白您为什么使用完全没有.这只会减慢已经很漫长的过程.

使用DirectoryInfoFileInfo类的文件和目录扫描的许多实现之一将更快.例如使用递归扫描目录 [ this [
The first thing is that I do not understand why you are using a TreeView at all. This will only slow down an already lengthy process.

It would be much faster to use one of the many implementations of a file and directory scan using the DirectoryInfo and FileInfo classes. For example Scan directories using recursion[^] which is in C# but there are plenty of free C# to VB translators on the web.

Secondly, if at all possible, you should avoid MD5. It is broken. Please read this[^] blog for the details.


我给了您以前的问题的答案,很多指示.我现在看不到该设计的痕迹,这是必须的.

文件结构项的数量可能太大,因此一次加载它们很容易消耗您的内存.我根据扩展/崩溃事件推荐了一种虚拟"树行为的模式.这段代码在哪里?

病毒扫描不是目录扫描,需要花费大量时间.因此,第一个问题是:您在哪里创建病毒扫描线程?如果您没有这样做,或者它与您的UI线程相同–那就忘记您的项目吧!

顺便说一下,关于Application.DoEvent的简单经验法则:永远不要使用它!我不完全理解为什么这么多的开发人员认为它可以帮助并一遍又一遍地犯下这个错误!不使用此API是CodeProject Questions上最受欢迎的主题之一.许多专家已经建议不要使用它.它可以在UI线程中工作,但是您至少需要两个并行执行线程.

这是您设计的外观:您需要创建一个额外的线程来进行病毒扫描.它应该一直工作并保持睡眠状态,直到您将其交给扫描任务为止.该线程应无限循环工作,应要求重复整个扫描周期.当用户单击扫描!"时它应将数据输入顶级扫描目录(加上选项)并唤醒线程,然后线程应开始扫描.用户界面应始终保持响应状态.对于扫描结果,您需要一个视图.它可以只是一个列表视图(比树更好),它的虚拟性质在我之前的回答中已经解释过:
需要Treeview Scan的一些专家帮助 [如何将ref参数传递给线程 [ Simple Blocking Queue中查看完整的示例代码.用于线程通信和线程间调用 [ ^ ].

另一种解决方案是在您每次有扫描请求时创建一个线程.由于一次只拥有一个线程(不计算UI线程),并且由于线程循环要花更多的时间而不是其创建时间,因此这并不重要.您可以通过System.Threading.Thread构造函数创建常规线程,使用线程池中的线程或使用System.ComponentModel.BackgroundWorker.

发现病毒后,线程应使用System.Windows.Forms.Control.BeginInvoke通知UI.您不能使用直接调用或访问任何属性来处理来自非UI线程的UI控件(读/写属性本质上也是一个调用).对BeginInvoke的调用会将调用所需的所有数据打包在队列中.此调用是非阻塞的,因此它在数据进入队列后立即返回到调用方,而不必等待委托的实际调用. UI在其主周期(Application.Run)中读取队列,并且实际调用在UI线程中完成. (原则上,了解此机制可以使您了解为什么DoEvent不好.)

这只是一个简短的概述.我可以看到你还没有走近.即使您的病毒专用代码已准备就绪,工作也相对繁琐.顺便说一句,您还应该将UI与功能的语义部分彻底隔离.恐怕我不太确定在自己学到比现在多得多的数量级之前,自己可以做到这一点.

—SA
I gave you the Answers to your previous Questions with a lot of instructions. I don''t see a trace of that design, which is a must, as I can see now.

The number of file structure items can be way too big, so loading them all at once can easily deplete you memory. I recommended a schema of "virtual" tree behavior based on the expanding/collapsing events; where is this code?

Virus scan is not a directory scan, it takes significant time. So, first question is: where do you create you virus-scanning thread? If you did not do it, or if this is the same as your UI thread — just forget your project!

By the way, the simple rule of thumb about Application.DoEvent: never ever use it! I do not fully understand why so many developer think it can help and make this mistake over and over! Not using this API is one of the most popular topics here at CodeProject Questions; many experts already recommended not using it. It works in the UI thread, but you need at least two parallel thread of execution.

Here is how your design should look: you need to create an extra thread for virus scan. It should work all the time and sleep until you give it a task to scan. The thread should work in infinite loop, repeating full scan cycle on request. When the user click "Scan!" it should feed the data on the top-level scan directory (plus options) and wake up the thread, and the thread should start scanning. You UI should stay responsive all the time. For the scan results, you need a view. It can be just a list view (better than the tree), the virtual nature of it I explained in my previous Answers: Is there a quicker way to list folders and subfolders?[^] and Need Some Expert Help With Treeview Scan Please[^].

I recommend using thread wrapper as shown in my sample code here: How to pass ref parameter to the thread[^]. This provides a safe way of delivering of any parameters the the thread. Similar idea can work for scheduling a thread pool thread or System.ComponentModel.BackgroundWorker (see below). Another way of feeding working data to a thread is a blocking queue. Please see full sample code in my Tip/Trick Article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

The alternative solution is to create a thread each time you have a request to scan. As you have only one thread at a time (not counting your UI thread) and because the thread cycle takes way more time then its creation, it does not really matter. You can create a regular thread via System.Threading.Thread constructor, use a thread from a thread pool or use System.ComponentModel.BackgroundWorker.

As a virus is found, the thread should notify UI using System.Windows.Forms.Control.BeginInvoke. You cannot handle UI controls from non-UI thread using direct calls or accessing any properties (reading/writing properties is essentially a call as well). The call to BeginInvoke will package all the data needed for the call in a queue. This call is non-blocking, so the it returns to the caller immediately after the data goes to the queue, not waiting for the actual call of the delegate. The queue is read by UI in its main cycle (of Application.Run) and actual calls are done in UI thread. (In principle, understanding of this mechanism can give you and idea of why DoEvent is bad.)

This is only a short overview. I can see you''re not even close so far. The work is relatively big even if your virus-specific code is ready. By the way, you should also thoroughly isolate you UI from semantic part of functionality. I''m afraid I''m not really sure that you can do it yourself before you learn an order of magnitude more compared to what you can do now.

—SA


这篇关于Treeview Scanner和MD5的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆