Results 1 to 8 of 8

Thread: [RESOLVED] Directory Items TO Listbox Items

  1. #1

    Thread Starter
    Member Undocked Windy's Avatar
    Join Date
    Mar 2009
    Location
    California
    Posts
    38

    Resolved [RESOLVED] Directory Items TO Listbox Items

    Any help would be greatly appreciated. I'm making an application that does some stuff involving the given subject, so this is very crucial to the completion and efficiency of my application.

    What I'm in need of today is simple (well, "seems" simple). I have one listbox that I want to populate with directories file's names. I don't mean the files inside the directories, but the names of the folders inside the directory.

    So, I have "c:\temp\dog", I'd click on my button or whatever and search for the directory and I choose "temp", said listbox would show "dog".

    Thanks, VBForums.

    UPDATE: Oh, BTW. It doesn't HAVE to be a listbox, it can be anything (IE: Textbox, Labels, Etc.).

    Home of Nuova Cartella (Mass Folder Creator)

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Directory Items TO Listbox Items

    Hey,

    Have you looked at the FolderBrowserDialog Class:

    http://msdn.microsoft.com/en-us/libr...serdialog.aspx

    Also, you might want to look at the System.IO.Directory Class:

    http://msdn.microsoft.com/en-us/libr...y_members.aspx

    There are members in there for doing exactly what you want.

    Hope that helps!!

    Gary

  3. #3
    Lively Member softkans's Avatar
    Join Date
    May 2006
    Posts
    70

    Re: Directory Items TO Listbox Items

    ''try to use

    vb Code:
    1. Dim di As New IO.DirectoryInfo("c:\temp")
    2.  
    3.         '' for adding directories
    4.         For Each d As IO.DirectoryInfo In di.GetDirectories
    5.             ListBox1.Items.Add(d.Name)
    6.         Next
    7.  
    8.         '' for adding files
    9.         For Each f As IO.FileInfo In di.GetFiles
    10.             ListBox1.Items.Add(f.Name)
    11.         Next

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Directory Items TO Listbox Items

    Something like
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         For Each FolderName As String In IO.Directory.GetDirectories("D:\", "*")
    3.             ListBox1.Items.Add(FolderName)
    4.         Next
    5. End Sub

  5. #5

    Thread Starter
    Member Undocked Windy's Avatar
    Join Date
    Mar 2009
    Location
    California
    Posts
    38

    Re: Directory Items TO Listbox Items

    Quote Originally Posted by softkans View Post
    ''try to use

    vb Code:
    1. Dim di As New IO.DirectoryInfo("c:\temp")
    2.  
    3.         '' for adding directories
    4.         For Each d As IO.DirectoryInfo In di.GetDirectories
    5.             ListBox1.Items.Add(d.Name)
    6.         Next
    7.  
    8.         '' for adding files
    9.         For Each f As IO.FileInfo In di.GetFiles
    10.             ListBox1.Items.Add(f.Name)
    11.         Next
    You think this might do?:

    Code:
    Dim gen
    Dim = inputbox("Directory Input:")
    Dim di As New IO.DirectoryInfo(gen)
            '' for adding directories
            For Each d As IO.DirectoryInfo In di.GetDirectories
                ListBox1.Items.Add(d.Name)
            Next
            '' for adding files
            For Each f As IO.FileInfo In di.GetFiles
                ListBox1.Items.Add(f.Name)
            Next

    Home of Nuova Cartella (Mass Folder Creator)

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Directory Items TO Listbox Items

    Hey,

    What are you expecting that code to do?

    I don't think it will even compile.

    I think what you are after is this:

    Code:
    Dim gen As String
    gen = inputbox("Directory Input:")
    Dim di As New IO.DirectoryInfo(gen)
    
    '' for adding directories
    For Each d As IO.DirectoryInfo In di.GetDirectories
        ListBox1.Items.Add(d.Name)
    Next
    
    '' for adding files
    For Each f As IO.FileInfo In di.GetFiles
        ListBox1.Items.Add(f.Name)
    Next
    Having said that though, why are you using an InputBox rather than a FolderBrowserDialog? Using an InputBox is prone to spelling mistakes etc.

    Also, you original post didn't mention looping through the files in the folder, just the directories. What exactly is it you are wanting?

    Hope that helps!!

    Gary

  7. #7

    Thread Starter
    Member Undocked Windy's Avatar
    Join Date
    Mar 2009
    Location
    California
    Posts
    38

    Re: Directory Items TO Listbox Items

    How could I substitute a inputbox with a folderbrowsedialog?

    I'm trying to get all folder names and get them inside a listbox. Basically, every "dir" name becomes a line in the listbox.



    UPDATE: I tried your code, it worked perfectly. Thread resolved. Although, I'm still interested in finding out the question to my answer above. :P
    Last edited by Undocked Windy; Mar 17th, 2009 at 08:59 AM.

    Home of Nuova Cartella (Mass Folder Creator)

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Directory Items TO Listbox Items

    All folder names on the entire drive?

    That wasn't the original question. Are you changing it 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