Для таких задач предназначен WMI, в .NET это пространство имен System.Management, вот код перечисляющий все CD-ROM'ы в системе:
ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
foreach(ManagementObject cdrom in objectSearcher.Get())
Console.WriteLine(cdrom.GetPropertyValue("Drive"));
Re: с помощью какой конструкции на c# вычислить букву cdrom
Здравствуйте, hellraiser, Вы писали:
H>с помощью какой конструкции на c# вычислить букву cdrom
/// <summary>
/// Describes available types for local drives
/// </summary>public enum DriveType : int
{
/// <summary>
/// The drive type cannot be determined
/// </summary>
Unknown = 0,
/// <summary>
/// The root path is invalid. For example, no volume is mounted at the path
/// </summary>
NotRootDir = 1,
/// <summary>
/// The disk can be removed from the drive.
/// </summary>
Removable = 2,
/// <summary>
/// The disk cannot be removed from the drive.
/// </summary>
Fixed = 3,
/// <summary>
/// The drive is a remote (network) drive.
/// </summary>
Remote = 4,
/// <summary>
/// The drive is a CD-ROM drive.
/// </summary>
CDROM = 5,
/// <summary>
/// The drive is a RAM disk.
/// </summary>
RamDisk = 6,
}
/// <summary>
/// Class for querying drives
/// </summary>public class Drive
{
[DllImport("kernel32", CharSet = CharSet.Auto)]
private extern static DriveType GetDriveType(string rootPath);
/// <summary>
/// Return array of all logical drives in system
/// </summary>
/// <returns>Array of strings, listing all logical drives in the local system</returns>public static string[] GetDrives()
{
return Directory.GetLogicalDrives();
}
/// <summary>
/// Return array of logical drives in local system of one of the specified types
/// </summary>
/// <param name="driveTypes">Array of driveTypes that should be listed</param>
/// <returns>Array of strings, listing logical drives of specified type(s)</returns>public static string[] GetDrives(DriveType[] driveTypes)
{
ArrayList drives = new ArrayList();
string[] alldrives = Directory.GetLogicalDrives();
foreach(string drive in alldrives)
{
DriveType dt = GetDriveType(drive);
foreach(DriveType match in driveTypes)
if (dt == match)
{
drives.Add(drive);
break;
}
}
return (string[])drives.ToArray(typeof(string));
}
/// <summary>
/// Return array of logical drives in local system of the specified type
/// </summary>
/// <param name="driveType">Type of drives that should be listed</param>
/// <returns>Array of strings, listing logical drives of specified type</returns>public static string[] GetDrives(DriveType driveType)
{
ArrayList drives = new ArrayList();
string[] alldrives = Directory.GetLogicalDrives();
foreach(string drive in alldrives)
{
DriveType dt = GetDriveType(drive);
if (dt == driveType)
drives.Add(drive);
}
return (string[])drives.ToArray(typeof(string));
}
}
Здравствуйте, serg_p, Вы писали:
_>Для таких задач предназначен WMI, в .NET это пространство имен System.Management, вот код перечисляющий все CD-ROM'ы в системе:
_>
_>ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
_>foreach(ManagementObject cdrom in objectSearcher.Get())
_> Console.WriteLine(cdrom.GetPropertyValue("Drive"));
_>
А почему в таком случае не работает следующий код:
ManagementObjectSearcher cdSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject cdRom in cdSearcher.Get())
listBox1.Items.Add(cdRom["VolumeSerialNumber"].ToString());
Здесь, конечно, не буква CD, но суть-то одна.
Re: с помощью какой конструкции на c# вычислить букву cdrom