views:

73

answers:

3

Hi everyone,

I've seen this code (and similar) all over the web, but I just cannot get it to work. Whenever I debug it line by line, it breaks out of debugging and loads the application. No error messages are presented, and any code after the "faulty" line remains unprocessed.

Here is the offending code:

foreach (string folder in allFolders)
{
    string[] subFolders = Directory.GetDirectories(folder, 
        "*", SearchOption.AllDirectories);
    MessageBox.Show("Test");
}

The foreach loop is entered into, but the message box is never displayed.

If I remove the SearchOption.AllDirectories the code is processed successfully, but I need some way to include all subdirectories within the directories.

Any ideas?

+1  A: 

MessageBox.Show is not working because your code is under web environment, while MessageBox is used in winform. Usually we use javascript to pop up a message box e.g. alert('hi').

Danny Chen
How did you know that questioner's code is under web environment?
MAKKAM
I suspect an educated guess.
Preet Sangha
@MAKKAM: It must be, because he debugged the code, stepped into the loop, but the `MessageBox` is now working without any exception. Probably he is debugging a web application.
Danny Chen
What about "If I remove the SearchOption.AllDirectories the code is processed successfully, but I need some way to include all subdirectories within the directories."?
Kobi
Yes, as Kobi said, questioner can show message box from his code.
MAKKAM
A: 

Tested your code and it's working OK, so the problem may be in another place of the code, or it may be a permission issue, although it returns an exception when it happens, the MSGBOX shows OK too.

        List<string> allFolders = new List<string>();
        allFolders.Add(@"C:\joomla\");

        foreach (string folder in allFolders)
        {
            string[] subFolders = Directory.GetDirectories(folder, "*", SearchOption.AllDirectories);
            MessageBox.Show("Test");
        }
sh4
+1  A: 

Your code works fine for me.
It seems to me that this method call just takes a lot of time to execute. For example, if there is a root directory in allFolders you have to wait several minutes (depends on your system parameters). Have you checked this code snippet on directories with just a few number of nested directories?
I assumed, that you work in winforms and execution just doesn't reach MessageBox.Show call.

MAKKAM
Thanks, I have a feeling this was the issue. I tried it on a folder with only a small number of folders and it seemed to work. I guess there were more subfolders in my original folder than I thought!
Zach Whitfield
Although even after leaving it for several minutes, the message box still doesn't display. Is it possible that because of the large number of subfolders that this code will never work (despite not producing an error)?
Zach Whitfield
I don't think that's possible. You should take into consideration that this method can take really long time to execute. Just to compare execution time: try to run your operating system embedded search from root directory with this wildcard mask and see how long this search runs.
MAKKAM