Click here to Skip to main content
15,358,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need Help With MD5 Treeview Scan!!

ok here it goes..... I currently have a program that I have been creating for quite some time. In the start I was using a listbox to populate the folders inside my selected directory. The listbox itterates down and the MD5 code checks each files Hash signature for virus but due to my large file system I have had to rethink how to go about scanning my directories as it takes to long to populate in a listview. The solution?....... Treeview!!!! but now im forced with the difficult task of replacing the listview with dirtreeview. I will post the code that I am working on. Thank you all in advance

  Using scanbox As New TextBox()
    Dim read As String = My.Computer.FileSystem.ReadAllText("virusSig.txt")


    detected.Text = Conversions.ToString(CheckedListBox1.Items.Count)
    files.Text = Conversions.ToString(ProgressBar1.Value)
    scanbox.Text = read.ToString

    Using md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
      Dim f As FileStream = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
      f = New FileStream(ListBox1.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

      Dim hash As Byte() = md5.Hash
      Dim buff As StringBuilder = New StringBuilder()
      Dim hashByte As Byte

      For Each hashByte In hash
        buff.Append(String.Format("{0:X2}", hashByte))

      If scanbox.Text.Contains(buff.ToString) Then
      End If
    End Using
  End Using
Catch ex As Exception
End Try

This code works great with listview but how can I get it to read with treeview?

I have had someone suggest to replace the listbox1.selecteditem with treeview.selectednode but errors unless i add .text or .tostring.......... can fullpath be used? im lost please help!

[Modified: added pre tags and properly formatted your code (added indents and spacing]
Updated 17-Feb-11 11:23am
William Winner 17-Feb-11 17:24pm
Why do you create the FileStream, set it as a New FileStream in the same line and then in the next line reset it as a New FileStrem? You're just wasting memory doing that and the first FileStream you created will not get cleaned up until you exit your program!

You only need to do it once!

Also, you created the variable read as a string. You don't then need to say read.ToString. That's redundant again.

Well, TreeView.SelectedNode returns a TreeNode. The version of the FileStream constructor you are using requires a String, so TreeView.SelectedNode will not work.

So, what's wrong with TreeView.SelectedNode.Text? Does that not work for you?
Dale 2012 17-Feb-11 18:08pm
I have tried using TreeView.SelectedNode.Text in the code above but it does not seem to check my virusSig.txt and no virus is detected. Any ideas on how to change this to get it working in treeview?
William Winner 18-Feb-11 12:39pm
Have you stepped through your code? Are you getting what you would expect from each of the steps? Does TreeView.SelectedNode.Text return a valid file name? Does md5.Hash have what you are expecting to find in it? Does buff have anything that is contained in the scanbox.Text string?
My best advice would be to avoid populating of the whole tree recursively at once. It can ultimately exhaust you memory. Better, do the following.

You have two trees: one is you tree view, another is the real directory structure in file system. I'll call it directory tree. You need to implement some virtual behavior: only populate tree nodes with the item at the moment you expand the node. Remove items after you collapse the node. At the same time, you need to keep impression that a logically empty node looks like it represents file structure underneath. For this purpose, use just one fake child node.

Let's see.

1) At first, show just one level of directory tree in your tree view. You only need to tell the difference between empty and non-empty sub-directories. If a sub-directory not empty, don't populate a corresponding tree node. Instead, add just one fake child node; don't expand the node. Also, you can artificially hide some files/directories.

2) Handle events System.Windows.Forms.TreeView.OnBeforeExpand and OnAfterCollapse. If the user can expand the node, it means the correspondent directory tree node is not empty, due to step (1). Before expanding, remove your fake node and populate the node to be expanded using the algorithm described in (1). When the node is expanded, it will show next directory level at corresponding directory tree node.

3) Inversely, handle event System.Windows.Forms.TreeView.OnAfterCollapse. The node in question is collapsed. Add your fake node again, remove all other child nodes underneath.

4) You need a procedure to refresh the tree view, preserving all nodes states but showing changes in directory structure. This should be coupled with operation of removing and adding nodes (and correspondent directory tree nodes) if you need those.

In this way, you will display only the levels of directory structure shown by the set of expanding nodes plus one fake level to give the impression of showing full structure.

One more advanced step is handling situation when another process modifies the directory being displayed, so you want to refresh your view by catching this event. For this purpose, you need to use the class System.IO.FileSystemWatcher.

That's it! Not extremely easy but quite doable.

Good luck,
Sergey Alexandrovich Kryukov 17-Feb-11 19:37pm
Thank you for accepting my Answer. Will you consider voting as well?
Sergey Alexandrovich Kryukov 18-Feb-11 11:18am
Please also see my other answer for usability considerations:

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