views:

43

answers:

2

I'm trying to modify a program where there is a variable that stores all the specified file types in a String() variable. What I would like to do is to somehow append to this variable in any way if I want to search another directory or just grab another individual file. Any suggestions would be greatly appreciated.

//Grab files from a directory with the *.txt or *.log as specified in the Combo Box
Dim strFiles As String()
strFiles = System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, IO.SearchOption.AllDirectories)

EDIT: Edited to include code snippet used.

Dim strFiles As String()
Dim listFiles As List(Of String)(strFiles)

If (cmbtype.SelectedItem = "All") Then
    //Do stuff

     For index As Integer = 1 To cmbtype.Items.Count - 1
         Dim strFileTypes As String() = System.IO.Directory.GetFiles(txtSource.Text, cmbtype.Items(index), IO.SearchOption.AllDirectories)
     Next

    //Exit Sub
Else
    listFiles.Add(System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, IO.SearchOption.AllDirectories).ToString())
End If
+1  A: 
dim listFiles as list(of string)
listFiles = System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, IO.SearchOption.AllDirectories).ToList()
listFiles.Add("..\blah\...\")
asawyer
+1: I was just typing something similar and then it popped up 1 new answer.
Ardman
I don't see a .ToList() option.
Seb
do you have Import System.Linq?
asawyer
I suppose if your using .Net 2.0 that wouldn't work.Luckly List(of T) will take an array in a constructor overload.Dim strFiles As String() strFiles = System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, IO.SearchOption.AllDirectories)dim listOfFiles as new list(of string)(strFiles)listOfFiles.Add("..\file\..")
asawyer
Tried to do that, but got an error about 'array bounds cannot be in type specifiers'. I'm trying to do this in Visual Studio 2008 with .Net 3.5
Seb
Sounds like you've got the generic type specifier and the constructor mixed up.It should be Dim alist As New List(of Some_Type)(Constructor_Args_Here)Can you post the snippet you tried?
asawyer
+3  A: 

Right now you're using a String() which is an array of String instances. Arrays are not well suited for dynamically growing structures. A much better type is List(Of String). It is used in very similar manners to a String() but has a handy Add and AddRange method for appending data to the end.

Dim strFiles As List(Of String)
strFiles.AddRange(System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, I

O.SearchOption.AllDirectories)

JaredPar