1、如何得到硬盘序列号C#如何得到硬盘序列号C#部门: xxx 时间: xxx整理范文,仅供参考,可下载自行编辑硬盘序列号Serial Number)不等于卷标号来获取硬盘信息,而不需要写VXD或者DRIVER。这样这个问题就解决了,我对它进行了封装,大量使用了P/Invoke技术,一个完整的Library。支持Windows 98-2003。b5E2RGbCAP使用上很简单:HardDiskInfo hdd = AtapiDevice.GetHddInfo(0。 / 第一个硬盘p1EanqFDPwConsole.WriteLine(Module Number: 0, hdd.ModuleNumb
2、er。DXDiTa9E3dConsole.WriteLine(Serial Number: 0, hdd.SerialNumber。RTCrpUDGiTConsole.WriteLine(Firmware: 0, hdd.Firmware。Console.WriteLine(Capacity: 0 M, hdd.Capacity。5PCzVD7HxA下面是全部代码:using System。using System.Runtime.InteropServices。using System.Text。namespace Sunmast.HardwareSerializablepublic str
3、uct HardDiskInfo/ / 型号/ public string ModuleNumber。/ / 固件版本/ public string Firmware。/ / 序列号/ public string SerialNumber。/ / 容量,以M为单位/ public uint Capacity。#region Internal StructsStructLayout(LayoutKind.Sequential, Pack=1internal struct GetVersionOutParamspublic byte bVersion。public byte bRevision。p
4、ublic byte bReserved。public byte bIDEDeviceMap。public uint fCapabilities。MarshalAs(UnmanagedType.ByValArray, SizeConst=4public uint dwReserved。 / For future use.StructLayout(LayoutKind.Sequential, Pack=1internal struct IdeRegspublic byte bFeaturesReg。public byte bSectorCountReg。public byte bSectorNu
5、mberReg。public byte bCylLowReg。public byte bCylHighReg。public byte bDriveHeadReg。public byte bCommandReg。public byte bReserved。StructLayout(LayoutKind.Sequential, Pack=1internal struct SendCmdInParamspublic uint cBufferSize。public IdeRegs irDriveRegs。public byte bDriveNumber。MarshalAs(UnmanagedType.
6、ByValArray, SizeConst=3public byte bReserved。MarshalAs(UnmanagedType.ByValArray, SizeConst=4public uint dwReserved。public byte bBuffer。StructLayout(LayoutKind.Sequential, Pack=1internal struct DriverStatuspublic byte bDriverError。public byte bIDEStatus。MarshalAs(UnmanagedType.ByValArray, SizeConst=2
7、public byte bReserved。MarshalAs(UnmanagedType.ByValArray, SizeConst=2public uint dwReserved。StructLayout(LayoutKind.Sequential, Pack=1internal struct SendCmdOutParamspublic uint cBufferSize。public DriverStatus DriverStatus。public IdSector bBuffer。StructLayout(LayoutKind.Sequential, Pack=1, Size=512j
8、LBHrnAILginternal struct IdSectorpublic ushort wGenConfig。public ushort wNumCyls。public ushort wReserved。public ushort wNumHeads。public ushort wBytesPerTrack。public ushort wBytesPerSector。public ushort wSectorsPerTrack。MarshalAs(UnmanagedType.ByValArray, SizeConst=3public ushort wVendorUnique。Marsha
9、lAs(UnmanagedType.ByValArray, SizeConst=20xHAQX74J0Xpublic byte sSerialNumber。public ushort wBufferType。public ushort wBufferSize。public ushort wECCSize。MarshalAs(UnmanagedType.ByValArray, SizeConst=8public byte sFirmwareRev。MarshalAs(UnmanagedType.ByValArray, SizeConst=40LDAYtRyKfEpublic byte sMode
10、lNumber。public ushort wMoreVendorUnique。public ushort wDoubleWordIO。public ushort wCapabilities。public ushort wReserved1。public ushort wPIOTiming。public ushort wDMATiming。public ushort wBS。public ushort wNumCurrentCyls。public ushort wNumCurrentHeads。public ushort wNumCurrentSectorsPerTrack。public ui
11、nt ulCurrentSectorCapacity。public ushort wMultSectorStuff。public uint ulTotalAddressableSectors。public ushort wSingleWordDMA。public ushort wMultiWordDMA。MarshalAs(UnmanagedType.ByValArray, SizeConst=128Zzz6ZB2Ltkpublic byte bReserved。#endregion/ / ATAPI驱动器相关/ public class AtapiDevice#region DllImpor
12、tDllImport(kernel32.dll, SetLastError=truestatic extern int CloseHandle(IntPtr hObject。DllImport(kernel32.dll, SetLastError=truestatic extern IntPtr CreateFile(string lpFileName,uint dwDesiredAccess,uint dwShareMode,IntPtr lpSecurityAttributes,uint dwCreationDisposition,uint dwFlagsAndAttributes,Int
13、Ptr hTemplateFile。DllImport(kernel32.dllstatic extern int DeviceIoControl(IntPtr hDevice,uint dwIoControlCode,IntPtr lpInBuffer,uint nInBufferSize,ref GetVersionOutParams lpOutBuffer,uint nOutBufferSize,ref uint lpBytesReturned,Out IntPtr lpOverlapped。DllImport(kernel32.dllstatic extern int DeviceIo
14、Control(IntPtr hDevice,uint dwIoControlCode,ref SendCmdInParams lpInBuffer,uint nInBufferSize,ref SendCmdOutParams lpOutBuffer,uint nOutBufferSize,ref uint lpBytesReturned,Out IntPtr lpOverlapped。const uint DFP_GET_VERSION = 0x00074080。const uint DFP_SEND_DRIVE_COMMAND = 0x0007c084。const uint DFP_RE
15、CEIVE_DRIVE_DATA = 0x0007c088。const uint GENERIC_READ = 0x80000000。const uint GENERIC_WRITE = 0x40000000。const uint FILE_SHARE_READ = 0x00000001。const uint FILE_SHARE_WRITE = 0x00000002。const uint CREATE_NEW = 1。const uint OPEN_EXISTING = 3。#endregion#region GetHddInfo/ / 获得硬盘信息/ / 硬盘序号/ 硬盘信息/ / 参考l
16、u0的文章:http:/lu0s1.3322.org/App/2k1103.html/ by sunmast for everyone/ thanks lu0 for his great works/ 在Windows 98/ME中,S.M.A.R.T并不缺省安装,请将SMARTVSD.VXD拷贝到%SYSTEM%IOSUBSYS目录下。dvzfvkwMI1/ 在Windows 2000/2003下,需要Administrators组的权限。/ / / AtapiDevice.GetHddInfo(/ public static HardDiskInfo GetHddInfo(byte dri
17、veIndexrqyn14ZNXIswitch(Environment.OSVersion.Platformcase PlatformID.Win32Windows:return GetHddInfo9x(driveIndex。case PlatformID.Win32NT:return GetHddInfoNT(driveIndex。case PlatformID.Win32S:throw new NotSupportedException(Win32s is not supported.。EmxvxOtOcocase PlatformID.WinCE:throw new NotSuppor
18、tedException(WinCE is not supported.。SixE2yXPq5default:throw new NotSupportedException(Unknown Platform.。6ewMyirQFL#region GetHddInfo9xprivate static HardDiskInfo GetHddInfo9x(byte driveIndexkavU42VRUsGetVersionOutParams vers = new GetVersionOutParams(。y6v3ALoS89SendCmdInParams inParam = new SendCmd
19、InParams(。SendCmdOutParams outParam = new SendCmdOutParams(。M2ub6vSTnPuint bytesReturned = 0。IntPtr hDevice = CreateFile(.Smartvsd,0,0,IntPtr.Zero,CREATE_NEW,0,IntPtr.Zero。if (hDevice = IntPtr.Zerothrow new Exception(Open smartvsd.vxd failed.。if (0 = DeviceIoControl(hDevice,DFP_GET_VERSION,IntPtr.Ze
20、ro,0,ref vers,(uintMarshal.SizeOf(vers,ref bytesReturned,IntPtr.ZeroCloseHandle(hDevice。throw new Exception(DeviceIoControl failed:DFP_GET_VERSION。0YujCfmUCw/ If IDE identify command not supported, failsif (0 = (vers.fCapabilities & 1CloseHandle(hDevice。throw new Exception(Error: IDE identify comman
21、d not supported.。eUts8ZQVRdif (0 != (driveIndex & 1inParam.irDriveRegs.bDriveHeadReg = 0xb0。elseinParam.irDriveRegs.bDriveHeadReg = 0xa0。if (0 != (vers.fCapabilities & (16 driveIndexsQsAEJkW5T/ We dont detect a ATAPI device.CloseHandle(hDevice。throw new Exception(string.Format(Drive 0 is a ATAPI dev
22、ice, we dont detect it,driveIndex + 1。GMsIasNXkAelseinParam.irDriveRegs.bCommandReg = 0xec。inParam.bDriveNumber = driveIndex。inParam.irDriveRegs.bSectorCountReg = 1。inParam.irDriveRegs.bSectorNumberReg = 1。inParam.cBufferSize = 512。if (0 = DeviceIoControl(hDevice,DFP_RECEIVE_DRIVE_DATA,ref inParam,(
23、uintMarshal.SizeOf(inParam,ref outParam,(uintMarshal.SizeOf(outParam,ref bytesReturned,IntPtr.ZeroCloseHandle(hDevice。throw new Exception(DeviceIoControl failed: DFP_RECEIVE_DRIVE_DATA。TIrRGchYzgCloseHandle(hDevice。return GetHardDiskInfo(outParam.bBuffer。#endregion#region GetHddInfoNTprivate static
24、HardDiskInfo GetHddInfoNT(byte driveIndex7EqZcWLZNXGetVersionOutParams vers = new GetVersionOutParams(。lzq7IGf02ESendCmdInParams inParam = new SendCmdInParams(。SendCmdOutParams outParam = new SendCmdOutParams(。zvpgeqJ1hkuint bytesReturned = 0。/ We start in NT/Win2000IntPtr hDevice = CreateFile(strin
25、g.Format(.PhysicalDrive0,driveIndex,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,IntPtr.Zero,OPEN_EXISTING,0,IntPtr.Zero。if (hDevice = IntPtr.Zerothrow new Exception(CreateFile faild.。if (0 = DeviceIoControl(hDevice,DFP_GET_VERSION,IntPtr.Zero,0,ref vers,(uintMarshal.SizeOf(vers,r
26、ef bytesReturned,IntPtr.ZeroCloseHandle(hDevice。throw new Exception(string.Format(Drive 0 may not exists.,driveIndex + 1。NrpoJac3v1/ If IDE identify command not supported, failsif (0 = (vers.fCapabilities & 1CloseHandle(hDevice。throw new Exception(Error: IDE identify command not supported.。1nowfTG4K
27、I/ Identify the IDE drivesif (0 != (driveIndex & 1inParam.irDriveRegs.bDriveHeadReg = 0xb0。elseinParam.irDriveRegs.bDriveHeadReg=0xa0。if (0 != (vers.fCapabilities & (16 driveIndexfjnFLDa5Zo/ We dont detect a ATAPI device.CloseHandle(hDevice。throw new Exception(string.Format(Drive 0 is a ATAPI device
28、, we dont detect it.,driveIndex + 1。tfnNhnE6e5elseinParam.irDriveRegs.bCommandReg = 0xec。inParam.bDriveNumber = driveIndex。inParam.irDriveRegs.bSectorCountReg = 1。inParam.irDriveRegs.bSectorNumberReg = 1。inParam.cBufferSize = 512。if (0 = DeviceIoControl(hDevice,DFP_RECEIVE_DRIVE_DATA,ref inParam,(uintMarshal.SizeOf(inParam,ref outParam,(uintMarshal.SizeOf(outParam,ref bytesR
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1