c# 无法获取子目录列表

发布于 2024-09-26 14:33:36 字数 457 浏览 0 评论 0原文

我在网上看到过这段代码(和类似的代码),但我就是无法让它工作。每当我逐行调试它时,它都会中断调试并加载应用程序。不会显示任何错误消息,并且“错误”行之后的任何代码都保持未处理状态。

这是有问题的代码:

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

进入 foreach 循环,但从未显示消息框。

如果我删除 SearchOption.AllDirectories 代码将成功处理,但我需要某种方法来包含目录中的所有子目录。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-10-03 14:33:36

你的代码对我来说效果很好。
在我看来,这个方法调用只是需要花费很多时间来执行。例如,如果 allFolders 中有根目录,则必须等待几分钟(取决于您的系统参数)。您是否在只有少量嵌套目录的目录上检查过此代码片段?
我假设您在 winforms 中工作并且执行没有达到 MessageBox.Show 调用。

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.

三生殊途 2024-10-03 14:33:36

MessageBox.Show 不起作用,因为您的代码是在 web 环境下,而 MessageBox 是在 winform 中使用的。通常我们使用javascript来弹出一个消息框,例如alert('hi')

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').

錯遇了你 2024-10-03 14:33:36

测试了你的代码,它工作正常,所以问题可能出在代码的另一个地方,或者可能是权限问题,虽然它发生时返回异常,但MSGBOX也显示正常。

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

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");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文