How do I enumerate available drives.

  • Thread starter Thread starter Phillip N Rounds
  • Start date Start date
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
 
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
 
Hi Phillip,

To enumerate the drives you can use...

System.IO.Directory.GetLogicalDrives();

John
 
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
 
Back
Top