estimating drive types

  • Thread starter Thread starter Matthias Heise
  • Start date Start date
M

Matthias Heise

Hello,

I just got my drives through Directory.GetLogicalDrives() but now I want to
distinguesh between real harddisk, floppy, CD-Rom or network drive. I didn't
find any good idea for that.

Can anyone help me with that?

Thanks

Matthias
 
Hi,
You can import GetDriveType from kernel32.dll
[DllImport("kernel32")]
static extern int GetDriveType(string path);

it returns the drive type :
const int DRIVE_CDROM = 5;
const int DRIVE_FIXED = 3;
const int DRIVE_NO_ROOT = 1;
const int DRIVE_REMOTE = 4;
const int DRIVE_REMOVABLE = 2;
 
Hi,
You can import GetDriveType from kernel32.dll
[DllImport("kernel32")]
static extern int GetDriveType(string path);

it returns the drive type :
const int DRIVE_CDROM = 5;
const int DRIVE_FIXED = 3;
const int DRIVE_NO_ROOT = 1;
const int DRIVE_REMOTE = 4;
const int DRIVE_REMOVABLE = 2;
 
Thanks,

but now another problem. When I'm trying to get the subdirectories with
Dir.GetDirectories() the application chrashes when accessing a CD-Rom with
no media in it.

Is there any method to test if a media within the drive is present or if a
network drive is realy mounted?

Thanks again

Matthias
 
I have created this small console application using WMI with a WQL statement
to retrieve a list of drives.

using System;
using System.Management;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);

foreach (ManagementObject logicalDisk in searcher.Get())
{
Console.WriteLine("{0} has a DriveType of {1}",
logicalDisk["Name"], logicalDisk["DriveType"]);
}
Console.Read();
}
}
}

Gabriel Lozano-Morán
 
Forgot to mention the drivetypes:

0 -> Unknown
1 -> No Root Directory
2 -> Removable Disk
3 -> Local Disk
4 -> Network Drive
5 -> Compact Disc
6 -> RAM Disk

Search on the MSDN for the string "Win32_LogicalDisk" for more info.

Gabriel Lozano-Morán
 
Back
Top