简体   繁体   中英

c# Cannot get list of subdirectories

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?

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 eg alert('hi') .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM