c# 无法获取子目录列表
我在网上看到过这段代码(和类似的代码),但我就是无法让它工作。每当我逐行调试它时,它都会中断调试并加载应用程序。不会显示任何错误消息,并且“错误”行之后的任何代码都保持未处理状态。
这是有问题的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码对我来说效果很好。
在我看来,这个方法调用只是需要花费很多时间来执行。例如,如果 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.
MessageBox.Show
不起作用,因为您的代码是在 web 环境下,而MessageBox
是在 winform 中使用的。通常我们使用javascript来弹出一个消息框,例如alert('hi')
。MessageBox.Show
is not working because your code is under web environment, whileMessageBox
is used in winform. Usually we use javascript to pop up a message box e.g.alert('hi')
.测试了你的代码,它工作正常,所以问题可能出在代码的另一个地方,或者可能是权限问题,虽然它发生时返回异常,但MSGBOX也显示正常。
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.