2 Replies - 3634 Views - Last Post: 31 July 2012 - 11:07 AM Rate Topic: -----

#1 Korlando  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-June 12

Problem with Antivirus scan code

Posted 31 July 2012 - 05:38 AM

I am trying to create an antivirus with visual basic 2010. But my code isn't working very well.
My program scans only the folder that you have chosen. It doesn't scan the sub folders.And if it scan the sub folder, the next sub folder will not be scanned. Also it doesn't scan the drive C:\ because there aren't any files on the folder. How the scan goes...

Desktop //Scans
---Sub Folder //Scans
------Sub FOlder //Don't scan!
There are two buttons. Custom Scan and Full Scan.
Custom scan:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        TabControl1.SelectTab(1)
        Try
            For Each strDir As String In
            System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                    ListBox1.Items.Add(strFile)

                Next

            Next
        Catch ex As Exception
        End Try

        Timer1.Start()

Full scan
 FolderBrowserDialog1.SelectedPath = ("C:")

        Try
            For Each strDir As String In
            System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                    ListBox1.Items.Add(strFile)

                Next

            Next
        Catch ex As Exception
        End Try

        Timer1.Start()

Timer 1:
ProgressBar1.Maximum = Conversions.ToString(ListBox1.Items.Count)
        total.Text = Conversions.ToString(ListBox1.Items.Count)

        If Not ProgressBar1.Value = ProgressBar1.Maximum Then
            Try

                ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                TextBox1.Text = ListBox1.SelectedItem.ToString
            Catch ex As Exception
            End Try



            Try

                Dim scanbox As New TextBox
                Dim read As String = My.Computer.FileSystem.ReadAllText("viruslist.txt")
                ProgressBar1.Increment(1)
                detected.Text = Conversions.ToString(ListBox2.Items.Count)
                files.Text = Conversions.ToString(ProgressBar1.Value)
                scanbox.Text = read.ToString
                Dim 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)
                md5.ComputeHash(f)
                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))
                Next

                If scanbox.Text.Contains(buff.ToString) Then



                    ListBox2.Items.Add(ListBox1.SelectedItem)
                End If
            Catch ex As Exception
            End Try
        Else
            Timer1.Stop()
            MsgBox("Finished Scanning Folder!")
            TabControl1.SelectTab(3)
            If ListBox1.Items.Count = 0 Then
                MsgBox("No Threats were detected, Scan Window will close!", MsgBoxStyle.Information)

            End If
        End If


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Antivirus scan code

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Problem with Antivirus scan code

Posted 31 July 2012 - 07:33 AM

Directory.GetFiles method has couple of overloads. Here is the one you should be using:
Directory.GetFiles(String, String, SearchOption)
http://msdn.microsof...y/ms143316.aspx

As for SearchOption parameter, use SearchOption.AllDirectories
http://msdn.microsof...y/ms143448.aspx

This will give you all the files inside the directory, including sub directories.

ps.
Maybe you should consider doing something else than an anti-virus software if you struggle with a problem like that...
Was This Post Helpful? 0
  • +
  • -

#3 Korlando  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 09-June 12

Re: Problem with Antivirus scan code

Posted 31 July 2012 - 11:07 AM

View Postjanne_panne, on 31 July 2012 - 07:33 AM, said:

Directory.GetFiles method has couple of overloads. Here is the one you should be using:
Directory.GetFiles(String, String, SearchOption)
http://msdn.microsof...y/ms143316.aspx

As for SearchOption parameter, use SearchOption.AllDirectories
http://msdn.microsof...y/ms143448.aspx

This will give you all the files inside the directory, including sub directories.

ps.
Maybe you should consider doing something else than an anti-virus software if you struggle with a problem like that...


Thanks for replaying. To be honest it didn't worked as I wish. It doesn't scan the full drive. BUT this code is actually better than mine. When I searched the desktop with this code, it scanned 20 files more! Yes it actually scanned all the sub folders. However I still got the same promblem
I made this update:
System.IO.Directory.GetFiles(strDir, "*.*", SearchOption.AllDirectories)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1