1

How will I store list of directories into an array? I've tried this method but this doesn't work.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim parentinfo As New DirectoryInfo("C:\myfiles")

    counter1 = My.Computer.FileSystem.GetFiles("C:\myfiles")
    dirsize = counter1.Count

    ReDim directory(dirsize)

    For Each dir As FileSystemInfo In parentinfo.GetFileSystemInfos()
        Dim i As Integer = 0
        directory(i) = dir.Name
        ComboBox1.Items.Add(directory(i))
        i += 1
    Next dir

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
mmr
  • 516
  • 11
  • 30

1 Answers1

0

Try doing something like this:

Public Sub New()

    Dim directories As New List(Of DirectoryInfo)

    Call Me.InitializeComponent()

    Call Me.ComboBox1.Items.Clear()
    For Each directory As DirectoryInfo In (New DirectoryInfo("C:\")).GetDirectories()
        Call directories.Add(directory)
        Me.ComboBox1.Items.Add(directory.Name)
    Next

End Sub

This will generate a list directories of DirectoryInfo objects for all directories found in the specified path. It will also add all directory names to the combobox (in this case Combobox1.

UPDATE

Just realised that you asked for an array specifically. You can transform a list to array by using the ToArray() extension method. So in this case at the end of the sub you can just call directories.ToArray() to get your array. Alternatively you can use the code I wrote with an array, but frankly, why use an array if you can use a list?

If you want to do it with an array here is the code:

Public Sub New()

    Dim i as integer

    Call Me.InitializeComponent()

    ReDim directory(i)
    For Each dir As DirectoryInfo In (New DirectoryInfo("C:\")).GetDirectories()
        directory(i) = dir
        Me.ComboBox1.Items.Add(directory.Name)
        i += 1
        ReDim Preserve directory(i)
    Next

End Sub

But I think it's a pain in the backside to do it this way...

yu_ominae
  • 2,975
  • 6
  • 39
  • 76
  • how will i declare directory? it gives an error when defined as Dim directory As New List(Of DirectoryInfo) – mmr Mar 19 '13 at 08:49
  • What is the error you are getting? Since you have defined directory globally as well, try giving the local variable a different name, or define the global variable as a List instead of an array. – yu_ominae Mar 19 '13 at 09:05
  • i followed my pattern to read directories.. but this help me a lot to understand array. – mmr Mar 19 '13 at 09:10
  • thanks yu_ominae..it is ok now. though i have to remove Call Me. InitializeComponent..doesn't work in mine when calling combobx. =) – mmr Mar 19 '13 at 09:13
  • Ah, sorry, that's a left-over from the form I used to test your code. I put mine in the `New()` sub, which then requires the `InitializeComponent()` call. By the way, be careful when you use the `Load` event to do stuff, because it has a bug which makes it swallow exceptions (see here http://stackoverflow.com/questions/3209706/why-the-form-load-cant-catch-exception). – yu_ominae Mar 19 '13 at 09:16