简体   繁体   中英

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!

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. Put it on a background thread instead and have it interact with the UI through message posting ( BeginInvoke and such).

Even once that program do the task complete I had only the d drive dirs in the list box!

Again, yeah, you're excluding the system drive in your query, which is presumably C:\

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

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