1

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

2 Answers 2

3

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 New List(Of String)()
strFiles.AddRange(System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, I

O.SearchOption.AllDirectories)

Sign up to request clarification or add additional context in comments.

Comments

1
dim listFiles as list(of string)
listFiles = System.IO.Directory.GetFiles(txtSource.Text, cmbtype.SelectedItem, IO.SearchOption.AllDirectories).ToList()
listFiles.Add("..\blah\...\")

6 Comments

+1: I was just typing something similar and then it popped up 1 new answer.
I don't see a .ToList() option.
do you have Import System.Linq ?
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\..")
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.