How do I enumerate available drives.

P

Phillip N Rounds

I need to eumerate all available drives in a C# Windows Form application (
and then of course the directory tree, but that's not the problem)

My question is, how to I enumerate all the available drives?
My initial thought was along the lines of

DirectoryInfo di = new DirectoryInfo( "c:\\");
DirectoryInfo Root = new DirectoryInfo( di.Parent.Name.ToString() );
DirectoryInfo[] RootDrives = Root.GetDirectories();

This, of course, doesn't work as the parent of c:\ is null.

For my immediate needs, I could simply loop through a:\, b:\, c:\... z:\,
test to see if the new DirectoryInfo("x:\\") threw an error, and be
satisfied with that.
This seems, at least, to be crude. I also would like the ability to get UNC
shares.

Anyone have a solution?

Thanks
Phil
 
G

Guest

Phillip N Rounds said:
I need to eumerate all available drives in a C# Windows Form application (
and then of course the directory tree, but that's not the problem)

My question is, how to I enumerate all the available drives?
My initial thought was along the lines of

DirectoryInfo di = new DirectoryInfo( "c:\\");
DirectoryInfo Root = new DirectoryInfo( di.Parent.Name.ToString() );
DirectoryInfo[] RootDrives = Root.GetDirectories();

This, of course, doesn't work as the parent of c:\ is null.

For my immediate needs, I could simply loop through a:\, b:\, c:\... z:\,
test to see if the new DirectoryInfo("x:\\") threw an error, and be
satisfied with that.
This seems, at least, to be crude. I also would like the ability to get UNC
shares.

Anyone have a solution?

Thanks
Phil
 
G

Guest

Hi Phillip,

To enumerate the drives you can use...

System.IO.Directory.GetLogicalDrives();

John
 
H

Herfried K. Wagner [MVP]

Phillip N Rounds said:
My question is, how to I enumerate all the available drives?

\\\
string[] drives = Environment.GetLogicalDrives();
///
 
P

Phillip N Rounds

Thanks

Ssmoimo said:
Hi Phillip,

To enumerate the drives you can use...

System.IO.Directory.GetLogicalDrives();

John

Phillip N Rounds said:
I need to eumerate all available drives in a C# Windows Form application
(
and then of course the directory tree, but that's not the problem)

My question is, how to I enumerate all the available drives?
My initial thought was along the lines of

DirectoryInfo di = new DirectoryInfo( "c:\\");
DirectoryInfo Root = new DirectoryInfo( di.Parent.Name.ToString() );
DirectoryInfo[] RootDrives = Root.GetDirectories();

This, of course, doesn't work as the parent of c:\ is null.

For my immediate needs, I could simply loop through a:\, b:\, c:\...
z:\,
test to see if the new DirectoryInfo("x:\\") threw an error, and be
satisfied with that.
This seems, at least, to be crude. I also would like the ability to get
UNC
shares.

Anyone have a solution?

Thanks
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top