【问题标题】:How to get all drives directories C#如何获取所有驱动器目录C#
【发布时间】:2020-06-18 14:29:33
【问题描述】:

我想获取所有驱动器目录并将它们放在一个列表框中,而不会导致系统崩溃。

我已尝试过此代码,但没有响应!即使该程序完成任务后,列表框中也只有 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);
        }
    }
}

【问题讨论】:

    标签: c# directory


    【解决方案1】:

    我试过这个代码,但没有响应

    是的,您在 UI 线程上投入了大量工作,当然 UI 没有响应。而是将其放在后台线程上,并通过消息发布(BeginInvoke 等)与 UI 进行交互。

    即使该程序完成任务后,列表框中也只有 d 驱动器目录!

    再次,是的,您在查询中排除了系统驱动器,大概是C:\

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

    【讨论】:

    • 阅读 C# 中的后台工作线程。网上有很多关于这个的文档。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多