简体   繁体   English

如何获取所有驱动器目录 C#

[英]How to get all drives directories C#

I want to get all drives directories and put them in a list box without system getting crashed.我想获取所有驱动器目录并将它们放在列表框中,而不会导致系统崩溃。

I've tried this codes but I get a not responding!我已经尝试过这些代码,但我没有响应! Even once that program do the task complete I had only the d drive dirs in the list box!即使该程序完成任务后,列表框中也只有 d 驱动器目录!

foreach (var drive in DriveInfo.GetDrives())
{
    if (drive.Name != Path.GetPathRoot(Environment.SystemDirectory))
    {
        foreach (string file in Directory.EnumerateFiles(drive.Name, "*.*", SearchOption.AllDirectories))
        {
            listBox1.Items.Add(file);
        }
    }
}

I've tried this codes but I get a not responding我已经尝试过此代码,但没有响应

Well yeah, you put an extreme amount of work on the UI thread, of course the UI is not responding.是的,您在 UI 线程上投入了大量工作,当然 UI 没有响应。 Put it on a background thread instead and have it interact with the UI through message posting ( BeginInvoke and such).将其放在后台线程上,并通过消息发布( BeginInvoke等)与 UI 进行交互。

Even once that program do the task complete I had only the d drive dirs in the list box!即使该程序完成任务后,列表框中也只有 d 驱动器目录!

Again, yeah, you're excluding the system drive in your query, which is presumably C:\同样,是的,您在查询中排除了系统驱动器,大概是C:\

if (drive.Name != Path.GetPathRoot(Environment.SystemDirectory))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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