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;
}

Arraylist and recursion problems
David Paynter
hi,
i don't see anything in your code except you use return 2 times , anyway its not a big deal
here its how to convert the array list to a typed array
private void button1_Click(object sender, EventArgs e)
{
string[] mysubFilesAndFolders = null;
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult rslt = fbd.ShowDialog();
if (rslt == DialogResult.OK)
{
mysubFilesAndFolders = GetSubItems(fbd.SelectedPath);
}
foreach (string itm in mysubFilesAndFolders)
{
listBox1.Items.Add(itm);
}
}
private string[] GetSubItems(string DirName)
{
ArrayList collection = new ArrayList();
DirectoryInfo dirinf = new DirectoryInfo(DirName);
DirectoryInfo[] subDirs = dirinf.GetDirectories();
FileInfo[] subFiles = dirinf.GetFiles();
foreach (DirectoryInfo dir in subDirs)
{
collection.Add("<Dir> " + dir.FullName);
}
foreach (FileInfo file in subFiles)
{
collection.Add("<File>" + file.FullName);
}
return (string[])collection.ToArray(typeof(string));
}
hope this helps
Kevin C. Kelly
needhelp1111
hi,
to use foreach() you need to have an object and a collection from the same type like foreach(string s in items), foreach(int i in numbers) ... etc
dirs is not type of arraylist so it will through exception
what do you want to acomplish by this code
best regards
Dimitry
Lenin82
Thanks I put that other return in for testing and forgot about it. Thanks for the help I was looking for something like that. I am getting another error now with the array list.
foreach
(ArrayList tmp in dirs)When I run this statement I get an exception error in the debuger that say's it cannont convert from type system.string to system.collection.arraylist. I think it's doing this because the value in dirs is the count. The array list also includes the list of the files that need to be stored but I can't get past the count problem. What do I need to do to get past this. Any help here would be great. If you need more code let me know and I'll paste it.