Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
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

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
01	Public Sub ScanScanner(ByVal dir As String)
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

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.. :)
Updated 20-Feb-11 16:51pm
Sandeep Mewara 21-Feb-11 3:16am    
Nicely framed and asked question.

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.
Share this answer
Sergey Alexandrovich Kryukov 20-Feb-11 22:13pm    
Henry, this is all useful (and you get my 5) but you probably don't know the pre-history of the topic. Please see the previous OP questions and my Answers (in particular). So far, we discussed both tree and list presentation (I don't know why not list, but this is not so critical). These view controls are needed to show results of the virus scan. I recommended a whole detained plan for "virtual" presentation (everything else can deplete memory and will be too slow). Now the problem is threading, of course. Where is it? I can see "Prohibited" DoEvents. I put part of the answer, will have to continue.
Thank you.
Espen Harlinn 21-Feb-11 9:40am    
Nice and to the point, my 5
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.

Share this answer
Yusuf 20-Feb-11 22:59pm    
I have to command you on your excellent answers, but I fail to understand that when people as asking for help it is meant to be Quick QA, not details help on their project
Sergey Alexandrovich Kryukov 20-Feb-11 23:23pm    
Thank you, Yusuf.
You're right, of course. Too many questions need the answers like: "hire a team lead" or "hire an architect" or a teacher, or even a software developer, so who knows what to answer? (Sometimes, exactly this.) But you know, this is not the worst case. Usually those who really need serious help are the best to deal with. Do you know when to expect a real blood-sucker? When someone starts with "I *only* need...", really... :-)
Dale 2012 21-Feb-11 0:07am    
Thank everyone for your expert help and opinions my problem at this point seems to be within my grasp and no further help with this topic is needed.

My greatest thanks goes to Sakryukov for his patients and detailed explinations

once again thank you everyone!! :)
Sergey Alexandrovich Kryukov 21-Feb-11 0:09am    
Good, thanks for accepting my Answer.
Espen Harlinn 21-Feb-11 9:40am    
Amazing effort, my 5++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900