post icon

Leer información de los discos duros con C#

Hoy estaba por el foro del MSDN y vi un trozo de código más que interesante posteado por Angel Jimenez para leer la información del hardware, específicamente de los discos duros desde C# valiéndose del sistema operativo.

No es muy común necesitar ésto pero suelen existir casos que requiramos algo así, el código nos muestra mucha información de los cuales los más relevantes se podrían decir que son el número de serie del dispositivo, la descripción, el ID, el tipo de Interfaz (IDE, SATA, etc), tamaño, tamaño por sector, el estado entre muchos otros datos más.

Arriba he puesto los enlaces del foro y del autor pero de igual manera se los transcribo aquí abajo.

using System;
using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_DiskDrive");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_DiskDrive instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Availability: {0}", queryObj["Availability"]);
                    Console.WriteLine("BytesPerSector: {0}", queryObj["BytesPerSector"]);

                    if (queryObj["Capabilities"] == null)
                        Console.WriteLine("Capabilities: {0}", queryObj["Capabilities"]);
                    else
                    {
                        UInt16[] arrCapabilities = (UInt16[])(queryObj["Capabilities"]);
                        foreach (UInt16 arrValue in arrCapabilities)
                        {
                            Console.WriteLine("Capabilities: {0}", arrValue);
                        }
                    }

                    if (queryObj["CapabilityDescriptions"] == null)
                        Console.WriteLine("CapabilityDescriptions: {0}", queryObj["CapabilityDescriptions"]);
                    else
                    {
                        String[] arrCapabilityDescriptions = (String[])(queryObj["CapabilityDescriptions"]);
                        foreach (String arrValue in arrCapabilityDescriptions)
                        {
                            Console.WriteLine("CapabilityDescriptions: {0}", arrValue);
                        }
                    }
                    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                    Console.WriteLine("CompressionMethod: {0}", queryObj["CompressionMethod"]);
                    Console.WriteLine("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
                    Console.WriteLine("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
                    Console.WriteLine("CreationClassName: {0}", queryObj["CreationClassName"]);
                    Console.WriteLine("DefaultBlockSize: {0}", queryObj["DefaultBlockSize"]);
                    Console.WriteLine("Description: {0}", queryObj["Description"]);
                    Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]);
                    Console.WriteLine("ErrorCleared: {0}", queryObj["ErrorCleared"]);
                    Console.WriteLine("ErrorDescription: {0}", queryObj["ErrorDescription"]);
                    Console.WriteLine("ErrorMethodology: {0}", queryObj["ErrorMethodology"]);
                    Console.WriteLine("FirmwareRevision: {0}", queryObj["FirmwareRevision"]);
                    Console.WriteLine("Index: {0}", queryObj["Index"]);
                    Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
                    Console.WriteLine("InterfaceType: {0}", queryObj["InterfaceType"]);
                    Console.WriteLine("LastErrorCode: {0}", queryObj["LastErrorCode"]);
                    Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]);
                    Console.WriteLine("MaxBlockSize: {0}", queryObj["MaxBlockSize"]);
                    Console.WriteLine("MaxMediaSize: {0}", queryObj["MaxMediaSize"]);
                    Console.WriteLine("MediaLoaded: {0}", queryObj["MediaLoaded"]);
                    Console.WriteLine("MediaType: {0}", queryObj["MediaType"]);
                    Console.WriteLine("MinBlockSize: {0}", queryObj["MinBlockSize"]);
                    Console.WriteLine("Model: {0}", queryObj["Model"]);
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("NeedsCleaning: {0}", queryObj["NeedsCleaning"]);
                    Console.WriteLine("NumberOfMediaSupported: {0}", queryObj["NumberOfMediaSupported"]);
                    Console.WriteLine("Partitions: {0}", queryObj["Partitions"]);
                    Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]);

                    if (queryObj["PowerManagementCapabilities"] == null)
                        Console.WriteLine("PowerManagementCapabilities: {0}", queryObj["PowerManagementCapabilities"]);
                    else
                    {
                        UInt16[] arrPowerManagementCapabilities = (UInt16[])(queryObj["PowerManagementCapabilities"]);
                        foreach (UInt16 arrValue in arrPowerManagementCapabilities)
                        {
                            Console.WriteLine("PowerManagementCapabilities: {0}", arrValue);
                        }
                    }
                    Console.WriteLine("PowerManagementSupported: {0}", queryObj["PowerManagementSupported"]);
                    Console.WriteLine("SCSIBus: {0}", queryObj["SCSIBus"]);
                    Console.WriteLine("SCSILogicalUnit: {0}", queryObj["SCSILogicalUnit"]);
                    Console.WriteLine("SCSIPort: {0}", queryObj["SCSIPort"]);
                    Console.WriteLine("SCSITargetId: {0}", queryObj["SCSITargetId"]);
                    Console.WriteLine("SectorsPerTrack: {0}", queryObj["SectorsPerTrack"]);
                    Console.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]);
                    Console.WriteLine("Signature: {0}", queryObj["Signature"]);
                    Console.WriteLine("Size: {0}", queryObj["Size"]);
                    Console.WriteLine("Status: {0}", queryObj["Status"]);
                    Console.WriteLine("StatusInfo: {0}", queryObj["StatusInfo"]);
                    Console.WriteLine("SystemCreationClassName: {0}", queryObj["SystemCreationClassName"]);
                    Console.WriteLine("SystemName: {0}", queryObj["SystemName"]);
                    Console.WriteLine("TotalCylinders: {0}", queryObj["TotalCylinders"]);
                    Console.WriteLine("TotalHeads: {0}", queryObj["TotalHeads"]);
                    Console.WriteLine("TotalSectors: {0}", queryObj["TotalSectors"]);
                    Console.WriteLine("TotalTracks: {0}", queryObj["TotalTracks"]);
                    Console.WriteLine("TracksPerCylinder: {0}", queryObj["TracksPerCylinder"]);
                }
            }
            catch (ManagementException e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

22 octubre 2012

Comentarios desde Facebook:

Sin Comentarios aun, puedes tú ser el primero en comentar!

Deja tu Comentario

Responder