Appending Directory.GetFiles Output to String[]

Status
Not open for further replies.

moderator99

Member
Joined
Mar 9, 2021
Messages
8
Programming Experience
Beginner
I'm not sure where to post this, so I'll post it here. Let me know if there's a better forum for me to post in.

I'm using Visual Studio 2019, creating a Windows Forms App under the .NET Framework in C#.

Here is my code at the moment:

C#:
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                string dir = fbd.SelectedPath;
                string[] dirPaths = Directory.GetDirectories(dir, "*.*", SearchOption.AllDirectories).Where(d => !d.EndsWith("_files") && !d.Contains("_files\\")).ToArray();
                foreach (string d in dirPaths)
                {
                    string[] filePaths = Directory.GetFiles(d, "*.*", SearchOption.TopDirectoryOnly);
                    foreach (string f in filePaths)
                    {
                        listBox1.Items.Add(f);
                        listBox1.Update();
                    }
                }
            }
        }

This is just part of a test program I made as I try to create a faster directory search routine that avoids reading through certain directories.

What I'm wanting to do is instead of adding the output from Directory.GetFiles to a list and creating a new string array for each iteration of the first foreach loop, I want to append the output to the same string array (filePaths) during each iteration of the first foreach loop. The listbox is only being used to view the output while I develop the directory search routine. What I need is a string array as the final result, to be used in a different program I've already pretty much finished developing that requires the output to be in a string array.

Please forgive my lack of understanding of the basic language protocols. I'm relatively new to programming. I've looked online for help and checked through the documentation, but I fail to find a solution to my problem.
 
For goodness sake, make up your mind! First you say that it's not the ListBox that's causing it to hang and then you say that it hangs on two lines that specifically populate the ListBox. That's the freakin' ListBox. If you remove the lines that refer to the ListBox and it doesn't hang then obviously it's the ListBox that's causing it to hang. You're seriously wasting our time now. As has already been said, you should just get rid of the ListBox altogether and stop using a fake UI to debug. VS already has a debugger so use that to debug.
 
BE SPECIFIC.
I was very specific in my OP.

Your reading comprehension is the problem here, not my writing or explaining the problem.

You didn't bother to concern yourself with why I use LINQ the way I did to exclude certain output. You changed that to something else that will not work in my case, and you didn't bother to explain why. That just introduces more problems down the road if I use it. So if you want to bitch about being specific....
 
I thought I understood what you were saying and it turns out I was right. I only doubted myself because you lied to us and told us that it wasn't the ListBox that was causing it to hang.
 
Status
Not open for further replies.
Back
Top Bottom