Ok I am new to this c# stuff. I have used c++ for a while and have to say that this new c# thing is pretty nice. Ok here is my problem. C# doesn't have vectors so I am using an arrylist instead. I don't know if you can set a type or not but I haven't. I store a list of files that I am recursively compiling as it runs through each directory. I was stupid and created a new instance of the array list each time I recursed. So I figured I need to make the arraylist global, but I don't know how to do this. Here is my code for the recursive directory listing. Let me know if you can spot something else I can do instead or if you can spot another error that I have not seen yet.
Thanks,
Matt
public ArrayList getDirectoryTreeFiles(string folderName) { //MessageBox.Show(folderName); DirectoryInfo di = new directoryInfo(folderName); DirectoryInfo [] dirNames = di.GetDirectories();
if (dirNames.Length != 0) { foreach (DirectoryInfo tmp in dirNames) { directoriesFilesString.Add(getDirectoryTreeFiles(folderName + "\\" + tmp.ToString())); //return directoriesFilesString; } } else { DirectoryInfo tmp = new DirectoryInfo(folderName); FileInfo[] rgFiles = tmp.GetFiles(); foreach(FileInfo fi in rgFiles) { directoriesFilesString.Add(folderName + "\\" + fi.Name); } return directoriesFilesString; } return directoriesFilesString; }
Ok I have figured this one out. At First I was creating a new instance of the array list every time I recursed. Also on line 9 I was saving the blank directory which was inserting a new listing of the count = function. Removing the .add function and just letting it recurse solved the problem.