Results 1 to 17 of 17

Thread: List of directories and files

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    List of directories and files

    I'm trying to make some functions to generate a list of everything in a directory I specify. I need for 1 list, all of the folders and then in the other list I need each file in the folders.

    How can I get all of the directories in a directory I specify and then put it in a list (or array, I could put the array into the list) and how can I get the list of all of the files in a directory?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Dim di As New DirectoryInfo("c:\")

    di.GetDirectories()
    di.GetFiles()

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    wow beaten to the post
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Button1_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] Button1.Click
    2.         [Color=Blue]Dim[/COLOR] strDirectories [Color=Blue]As[/COLOR] [Color=Blue]String[/COLOR]() = IO.Directory.GetDirectories("C:\")
    3.         ListBox1.Items.AddRange(strDirectories)
    4.     [Color=Blue]End[/COLOR] [Color=Blue]Sub
    5.  
    6. [/COLOR]    [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] ListBox1_MouseUp([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] [Color=Blue]Object[/COLOR], [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.Windows.Forms.MouseEventArgs) [Color=Blue]Handles[/COLOR] ListBox1.MouseUp
    7.         [Color=Blue]Try
    8. [/COLOR]            [Color=Blue]If[/COLOR] [Color=Blue]Not[/COLOR] ListBox1.SelectedIndex = -1 [Color=Blue]Then
    9. [/COLOR]                [Color=Blue]Dim[/COLOR] strFiles [Color=Blue]As[/COLOR] [Color=Blue]String[/COLOR]() = IO.Directory.GetFiles(ListBox1.SelectedItem)
    10.                 [Color=Blue]Dim[/COLOR] x [Color=Blue]As[/COLOR] [Color=Blue]Integer
    11. [/COLOR]                [Color=Blue]For[/COLOR] x = strFiles.GetLowerBound(0) [Color=Blue]To[/COLOR] strFiles.GetUpperBound(0)
    12.                     strFiles(x) = strFiles(x).Replace(ListBox1.SelectedItem, "")
    13.                 [Color=Blue]Next
    14. [/COLOR]                ListBox2.Items.Clear()
    15.                 ListBox2.Items.AddRange(strFiles)
    16.             [Color=Blue]End[/COLOR] [Color=Blue]If
    17. [/COLOR]        [Color=Blue]Catch[/COLOR] ex [Color=Blue]As[/COLOR] Exception
    18.             MessageBox.Show(ex.Message)
    19.         [Color=Blue]End[/COLOR] [Color=Blue]Try
    20. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    edit: nm, brain fart. Thanks

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    This doesn't seem to work at all, it gives me an excemption when I try to open the Form that will list this.

    It says Object reference not set to an instance of the object

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Which one doesn't work?

    crptcblade's code assumes you have an Imports System.IO, whether its in the form or project wide.

    dynamic_sysop's assumes you have a listbox named Listbox1 on the form the code belongs to. (i think you knew that though)

    Also the GetDirectories, GetFiles methods may return Nothing instead of an empty array if no results are found.

  7. #7

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Edneeis
    Which one doesn't work?

    crptcblade's code assumes you have an Imports System.IO, whether its in the form or project wide.

    dynamic_sysop's assumes you have a listbox named Listbox1 on the form the code belongs to. (i think you knew that though)

    Also the GetDirectories, GetFiles methods may return Nothing instead of an empty array if no results are found.
    I tried imports system.IO and it still won't work. None of my code gives me errors

    my ListBox is ListBox1 and I'm testing it on a directory with alot of diles (d so I dunno *** is going on.
    Code:
    Public Shared Function PopulateList1() As System.Windows.Forms.ListBox
                Dim ListBox1 As System.Windows.Forms.ListBox
                Dim strDirectories As String() = IO.Directory.GetDirectories("D:\")
                ListBox1.Items.AddRange(strDirectories)
                Return ListBox1
            End Function
    Note: I have this code in a DLL, but the DLL is references to my program and the program uses alot of functions in the DLL perfectly fine.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You never set ListBox1 to an instance, there is no new in there. Either stick it in the declaration or try this:
    VB Code:
    1. Public Shared Sub PopulateList1(ByRef lbo as ListBox)
    2.     lbo.Items.AddRange(IO.Directory.GetDirectories("D:\"))
    3. End Sub
    Last edited by Edneeis; Oct 30th, 2003 at 12:38 AM.

  9. #9

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Edneeis
    You never set ListBox1 to an instance, there is no new in there. Either stick it in the declaration or try this:
    VB Code:
    1. Public Shared Sub PopulateList1(ByRef lbo as ListBox)
    2.     lbo.AddRange(IO.Directory.GetDirectories("D:\"))
    3. End Sub
    I tried putting the listbox in the input of the function and using it, I also tried new when I create it within the function.

    Both still do the samething.

    Is there a problem setting the listbox in my program equal to the returned listbox from the DLL?

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What samething do they do? The Not set to an instance error? Which line is giving you the trouble? Where does the debugger freak out at? I'll stick it in a dll and try to recreate the problem.

    You have a reference to System.Windows.Forms in the dll right? Also I forgot 'Items' in my example (I edited it now).

    Also what kind of device is your D drive? CDROM? Hard Drive?
    Last edited by Edneeis; Oct 30th, 2003 at 12:40 AM.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well I ran the following code from a dll and everything worked fine. My D drive is a DVDROM.
    VB Code:
    1. 'in dll
    2.     Public Shared Sub PopulateList1(ByRef lbo As ListBox)
    3.         lbo.Items.AddRange(IO.Directory.GetDirectories("D:\"))
    4.     End Sub
    5.  
    6. 'in form
    7. TestDLL1.Tester.PopulateList1(ListBox1)

  12. #12

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Edneeis
    What samething do they do? The Not set to an instance error? Which line is giving you the trouble? Where does the debugger freak out at? I'll stick it in a dll and try to recreate the problem.

    You have a reference to System.Windows.Forms in the dll right? Also I forgot 'Items' in my example (I edited it now).

    Also what kind of device is your D drive? CDROM? Hard Drive?
    It never pointed me to a part of my code, otherwise I probably would of been able to fix this myself. It just stops and says there is no source code available for the debugger. That was in release mode, in the debugger mode, it bitches when I used form.showdialog() to show a form. It works perfectly fine if I don't use the code so I know that isn't the problem.
    Code:
    An unhandled exception of type 'System.NullReferenceException' occurred in Binary Edit.exe
    
    Additional information: Object reference not set to an instance of an object
    My D: is another hard drive(the main drive actually)

    and yes I have a reference to System.Windows.Forms in the DLL.

    Originally posted by Edneeis
    Well I ran the following code from a dll and everything worked fine. My D drive is a DVDROM.
    VB Code:
    1. 'in dll
    2.     Public Shared Sub PopulateList1(ByRef lbo As ListBox)
    3.         lbo.Items.AddRange(IO.Directory.GetDirectories("D:\"))
    4.     End Sub
    5.  
    6. 'in form
    7. TestDLL1.Tester.PopulateList1(ListBox1)
    Just tried that, still the EXACT same error, ugh.

    In my DLL:
    Code:
    Public Shared Sub PopulateList1(ByRef lbo As System.Windows.Forms.ListBox)
                lbo.Items.AddRange(IO.Directory.GetDirectories("D:\"))
            End Sub
    In my form:
    Code:
    Templates.TextType.Populate.PopulateList1(listboxTemplate1)
    Ugh, I don't get it.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Seems to be some sort of weirdness going on. Can you surround the code in a try catch and get the stack trace? Or from the debugger's Immediate window you might be able to get it. That should give you a history of what happened and what lines are muffin' up. Or if there is something you can post or email then I'll look at it further.

  14. #14

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    I don't even know how to do a stack trace.

    This is wierd though. My code was almost the same as what was posted when I was figuring this out, but it didn't work at all so I figured to ask. Hmm, mybe a bug in VB?

    Don't really have any code to give ya. Basically, I have a main form. You click a button and it opens a little form. On load the little form is supposed to use a function in the DLL to get all of the directories and files so it can list them in the form that calls it.

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    StackTrace is a property of the exception object. It lists some very useful information like the methods called and what file and line they are in/on.
    VB Code:
    1. Try
    2. 'code
    3. Catch Ex As Exception
    4. Msgbox(e.StackTrace)
    5. End Try

    It could be a bug but then why does it work for me. Maybe its something small that we overlooked. I'll post a working example and have you run it.

  16. #16

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Alright I fixed it out.

    Damn, that was stupid. With my MDI application, I'm opening and closing alot of different forms, figured I'd make a mistake opening a form.

    I forgot new for opening the form.

    Still wierd though cause it worked without it a few times

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yeah that is wierd but at least its fixed now.

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