Results 1 to 5 of 5

Thread: Getting used to IEnumerator's

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Question Getting used to IEnumerator's

    I'm trying to write a search routine that will search the hard drive for a list of files to see if they exist, I keep getting this error:

    An unhandled exception of type 'System.ArgumentException' occurred in search.exe

    Additional information: The parameter is incorrect.

    it crashes on this line:

    oFolder = oFS.GetFolder(oFolders(FolderKey.Current).Path.ToString)


    it also crashes on the msgbox line that is commented out, and I just don't see why.

    Public Function File_Search_Test( ByVal rs As ADODB.Recordset) As String

    Dim oFS As New Scripting.FileSystemObject()
    Dim oFolder As Scripting.Folder
    Dim oFolders As Scripting.Folders
    Dim oDrives As Scripting.Drives
    Dim oDrive As Scripting.Drive
    Dim iCount As Short
    Dim iFolder As Short
    Dim oKey As IEnumerator
    Dim FolderKey As IEnumerator

    oDrives = oFS.Drives

    oKey = oDrives.GetEnumerator

    Do Until oKey.MoveNext = False

    If oDrives(oKey.Current).DriveType.ToString = "Fixed" Then
    oFolders = oDrives(oKey.Current).RootFolder.SubFolders
    FolderKey = oFolders.GetEnumerator
    FolderKey.MoveNext()
    'MsgBox(oFolders.Item(FolderKey.Current).Name.ToString)
    oFolder = oFS.GetFolder(oFolders(FolderKey.Current).Path.ToString)
    MsgBox(oFolder.Name)

    End If
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    I think FolderKey.Current returns an Object. You may have to do something like this.

    Ctype(FolderKey.Current, Folder)

    That which does not kill us, only makes us stronger.

  3. #3
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    This will work better than my last suggestion. Is it what you were after?

    Public Function File_Search_Test() As String

    Dim oFS As New Scripting.FileSystemObject()
    Dim oFolder As Scripting.Folder
    Dim oFolders As Scripting.Folders
    Dim oDrives As Scripting.Drives
    Dim oDrive As Scripting.Drive
    Dim iCount As Short
    Dim iFolder As Short
    Dim oKey As IEnumerator
    Dim FolderKey As IEnumerator
    Dim CurrentFolder As Scripting.Folder

    oDrives = oFS.Drives

    oKey = oDrives.GetEnumerator

    Do Until oKey.MoveNext = False

    If oDrives(oKey.Current).DriveType.ToString = "Fixed" Then
    oFolders = oDrives(oKey.Current).RootFolder.SubFolders
    FolderKey = oFolders.GetEnumerator
    FolderKey.MoveNext()

    ''''''''''''''''''''''''''''''''''''''''''''''''
    CurrentFolder = CType(FolderKey.Current, Scripting.Folder)
    MsgBox(CurrentFolder.Name)
    '''''''''''''''''''''''''''''''''''''''''''''''''
    End If
    Loop
    End Function

    That which does not kill us, only makes us stronger.

  4. #4

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Yea I talked to one of my C# friends over the weekend, and the answer we came up with that worked was:

    FolderKey = oFolders.GetEnumerator
    FolderKey.MoveNext
    FolderKey.Current.Name.ToString

    Instead of

    FolderKey = oFolders.GetEnumerator
    FolderKey.MoveNext
    oFolders.Item(FolderKey.Current).Name.ToString


    That seems weird to me, becase this line works:

    oFolders = oDrives(oKey.Current).RootFolder.SubFolders

    From what I've learned either this should work:

    oFolders = oDrives(oKey.Current).RootFolder.SubFolders
    oFolders.Item(FolderKey.Current).Name.ToString

    Or I should have to do use:

    FolderKey.Current.Name.ToString
    oFolders = oKey.Current.RootFolder.SubFolders

    it just doesn't seem right that oFolders = oDrives(oKey.Current).RootFolder.SubFolders works while the other doesn't could this be a bug in VB.NET ?

    cType, I take it that is for casting? my C# friend kept asking me if I knew how to cast..
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    yeah CType is the closest equivalnt to C# casting..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width