List free drive letters

  • Thread starter Thread starter Guest
  • Start date Start date
Randy,

For this, I would call the GetLogicalDrives or GetLogicalDriveStrings
functions in the Win32 API through the P/Invoke layer. It will give you the
drive letters that are occupied. All you have to do is remove those from
the list of known letters (a-z) and you have your free drive letters.

Hope this helps.
 
Nicholas,

thanks a lot for your answer!! :-)

Randy


Nicholas Paldino said:
Randy,

For this, I would call the GetLogicalDrives or GetLogicalDriveStrings
functions in the Win32 API through the P/Invoke layer. It will give you the
drive letters that are occupied. All you have to do is remove those from
the list of known letters (a-z) and you have your free drive letters.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Randy said:
Hi,

how can I get a list of all "free" drive letters?

Thanks in advance, Randy
 
I would suggest this instead of PInvoke, if only because its managed code.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");

ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);

ManagementObjectCollection disks = searcher.Get();

foreach (ManagementObject disk in disks)

{

Console.WriteLine("Disk = " + disk["deviceid"]);

}


--
Jonathan Allen


Randy said:
Nicholas,

thanks a lot for your answer!! :-)

Randy


Nicholas Paldino said:
Randy,

For this, I would call the GetLogicalDrives or GetLogicalDriveStrings
functions in the Win32 API through the P/Invoke layer. It will give you
the
drive letters that are occupied. All you have to do is remove those from
the list of known letters (a-z) and you have your free drive letters.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Randy said:
Hi,

how can I get a list of all "free" drive letters?

Thanks in advance, Randy
 
Does

WqlObjectQuery objectQuery = new WqlObjectQuery("delete from
Win32_LogicalDisk where Drive='C'")

remove the drive? :)
 
Or better:
"select deviceid from Win32_LogicalDisk"
to prevent active select of the Floppy drive, all you need is the drive
letter (deviceid) right?

Willy.

Jonathan Allen said:
I would suggest this instead of PInvoke, if only because its managed code.

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");

ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);

ManagementObjectCollection disks = searcher.Get();

foreach (ManagementObject disk in disks)

{

Console.WriteLine("Disk = " + disk["deviceid"]);

}


--
Jonathan Allen


Randy said:
Nicholas,

thanks a lot for your answer!! :-)

Randy


Nicholas Paldino said:
Randy,

For this, I would call the GetLogicalDrives or
GetLogicalDriveStrings
functions in the Win32 API through the P/Invoke layer. It will give you
the
drive letters that are occupied. All you have to do is remove those
from
the list of known letters (a-z) and you have your free drive letters.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

how can I get a list of all "free" drive letters?

Thanks in advance, Randy
 
Maybe. I've just come across it myself, so I don't know all what you can do
with it.
 
Frank Rizzo said:
Does

WqlObjectQuery objectQuery = new WqlObjectQuery("delete from
Win32_LogicalDisk where Drive='C'")

remove the drive? :)

There is no such thing like "Delete" in WQL, the syntax is SQL like but only
a subset is used.

Willy.
 
Back
Top