Event Raise

mshahid

Active member
Joined
Aug 23, 2012
Messages
29
Programming Experience
Beginner
i am new in vb. I am trying to develop a program which scan files and raise event when new file begin but i got a warning message invalid case exception was unhandled.

following is the code

thanks in advance

Imports System.IO
Imports System.IO.Directory
Public Class DirScanner
Public Event ScanProgress(ByVal foldersScanned As Integer)
Private totalSize As Long
Private nFolders As String
Public Function ScanFolder(ByVal folder As Integer)
Dim file, dir As String
Dim FI As FileInfo
For Each file In Directory.GetFiles(folder)
FI =
New FileInfo(file)
totalSize = totalSize + FI.Length
Next
For Each dir In Directory.GetDirectoryRoot(folder)
nFolders = nFolders + 1
RaiseEvent ScanProgress(nFolders)
ScanFolder(dir)
Next
Return totalSize
End Function
End
Class


Public Class Form1
Dim WithEvents objDirScanner As DirScanner
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim folder As String
objDirScanner = New DirScanner()
folder =
"C:\Program files"
MsgBox("Your files occupy " & objDirScanner.ScanFolder(folder).ToString & " bytes on the drive")
End Sub
Private Sub objDirScanner_ScanProgress(ByVal foldersScanned As Integer) Handles objDirScanner.ScanProgress
Me.Text = "Scanned" & foldersScanned.ToString & " folders so far"
End Sub
End
Class
 
First things first, please don't post your code like that because it's very hard to read. Post it as plain text inside
 tags and the forum will format it.  Once we can read the code comfortably, we can see what might be wrong with it.
 
Hi,

I have spent some time sorting your code into a readable format but for future reference please can you follow jmcilhinney's lead and ensure that you load sample code with the correct formatting tags.

I have found 3 specific issues that you need to deal with. The main issue being point 3. I have also reloaded your class to point out where the issues are:-

1) You are passing a string parameter to your function Public Function ScanFolder(ByVal folder As Integer) but your have declared your parameter in the function as an integer this should be Public Function ScanFolder(ByVal folder As String)

2) You make a recursive call to ScanFolder(dir) after you have called GetDirectoryRoot of the current directory that you have scanned but this is in the wrong format for your own function and should be ScanFolder(dir & ":\")

3) You need to rethink the whole logic of what you are trying to achieve since this is an endless recursion of the function ScanFolder due to the fact that you have called GetDirectoryRoot with GetDirectoryRoot("C:\Program files") which equals C and then on the second and all future recursions of ScanFolder you call GetDirectoryRoot("C:\") which will always equal C and therefore you never leave the root directory and your program never completes. Well it will complete at some point by crashing.

Here's your class with my comments applied.

VB.NET:
Public Class DirScanner
  Public Event ScanProgress(ByVal foldersScanned As Integer)
  Private totalSize As Long
  Private nFolders As String
 
  'error 1
  'Public Function ScanFolder(ByVal folder As Integer)
  Public Function ScanFolder(ByVal folder As String)
    Dim file, dir As String
    Dim FI As FileInfo
    For Each file In Directory.GetFiles(folder)
      FI = New FileInfo(file)
      totalSize = totalSize + FI.Length
    Next
    For Each dir In Directory.GetDirectoryRoot(folder)
      nFolders = nFolders + 1
      RaiseEvent ScanProgress(nFolders)
      'error 2
      'the directory formatting returned by GetDirectoryRoot is incorrect for passing to ScanFolder
      'ScanFolder(dir)
      ScanFolder(dir & ":\")
      'error 3
      'this is endless recursion of the function ScanFolder due to the fact that you have called GetDirectoryRoot
      'where GetDirectoryRoot("C:\Program files") = C and GetDirectoryRoot("C:\") always equals C and therefore
      'you never leave the root directory
    Next
    Return totalSize
  End Function
End Class
Hope this helps.

Cheers,

Ian
 
I cannot recommend strongly enough that you turn Option Strict On for this project and also in the IDE options so it's On for all future projects. The compiler will then flag issues like passing the wrong data type to a method and also functions with no return type specified.
 
Back
Top