我如何才能找到没有.NET使用WMI的硬盘设备序列号?(How can I find the Har

2019-06-26 17:09发布

我想从硬盘的硬连线的序列号,但没有使用WMI。 我尝试使用WMI代码,它不会在我的机器肯定工作。 那么,有没有寻找物理硬盘的序列号在.NET中的任何选择吗?

Answer 1:

这应该有助于上手:

如何获得物理硬盘的序列号不WMI

关于你提到的问题,WMI不返回数据; 你知道如何正确的权限从WMI获取数据? 您可以使用WMI工具来检查/解决这个问题。



Answer 2:

使用的CreateFile ,如下图所示。 它可能需要管理permissions.Add 4文本框,并在你的代码的按钮。

 private const int CREATE_NEW = 1;
    private const int OPEN_EXISTING = 3;
    private const uint GENERIC_READ = 0x80000000; 
    private const uint GENERIC_WRITE = 0x40000000;
    // 10000000000000000000000000000000 --- GENERIC_READ
    // 01000000000000000000000000000000 --- GENERIC_WRITE
    // 00100000000000000000000000000000 --- GENERIC_EXECUTE
    // 00010000000000000000000000000000 --- GENERIC_ALL       

    //Securable objects use an access mask format in which the 
    //four high-order bits specify generic access rights


    private const int FILE_SHARE_READ = 0x1;      
    private const int FILE_SHARE_WRITE = 0x2;
    // 00000000000000000000000000000001 --- FILE_SHARE_READ
    // 00000000000000000000000000000010 --- FILE_SHARE_WRITE
    // 00000000000000000000000000000100 --- FILE_SHARE_DELETE 

    private const int VER_PLATFORM_WIN32_NT = 2;
    private const int DFP_RECEIVE_DRIVE_DATA = 0x7C088;
    //     0         000000000000111         11                  0               00000100010         00
    //     Common    Device Type             Required Access     Custom          Function Code       Transfer Type

    private const int INVALID_HANDLE_VALUE = -1;

    public enum DriveTypes { Fixed, Removable, Unknown };
    public string[] DriveStrings = { "Fixed", "Removable", "Unknown" };

    [StructLayout(LayoutKind.Sequential, Size = 8)]
    private class IDEREGS
    {
        public byte Features;
        public byte SectorCount;
        public byte SectorNumber;
        public byte CylinderLow;
        public byte CylinderHigh;
        public byte DriveHead;
        public byte Command;
        public byte Reserved;
    }

    [StructLayout(LayoutKind.Sequential, Size = 32)]
    private class SENDCMDINPARAMS
    {
        public int BufferSize;
        public IDEREGS DriveRegs;
        public byte DriveNumber;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] Reserved;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] Reserved2;
        public SENDCMDINPARAMS()
        {
            DriveRegs = new IDEREGS();
            Reserved = new byte[3];
            Reserved2 = new int[4];
        }
    }
    [StructLayout(LayoutKind.Sequential, Size = 12)]
    private class DRIVERSTATUS
    {
        public byte DriveError;
        public byte IDEStatus;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] Reserved;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public int[] Reserved2;
        public DRIVERSTATUS()
        {
            Reserved = new byte[2];
            Reserved2 = new int[2];
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private class IDSECTOR
    {
        public short GenConfig;
        public short NumberCylinders;
        public short Reserved;
        public short NumberHeads;
        public short BytesPerTrack;
        public short BytesPerSector;
        public short SectorsPerTrack;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public short[] VendorUnique;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public char[] SerialNumber;
        public short BufferClass;
        public short BufferSize;
        public short ECCSize;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public char[] FirmwareRevision;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
        public char[] ModelNumber;
        public short MoreVendorUnique;
        public short DoubleWordIO;
        public short Capabilities;
        public short Reserved1;
        public short PIOTiming;
        public short DMATiming;
        public short BS;
        public short NumberCurrentCyls;
        public short NumberCurrentHeads;
        public short NumberCurrentSectorsPerTrack;
        public int CurrentSectorCapacity;
        public short MultipleSectorCapacity;
        public short MultipleSectorStuff;
        public int TotalAddressableSectors;
        public short SingleWordDMA;
        public short MultiWordDMA;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 382)]
        public byte[] Reserved2;
        public IDSECTOR()
        {
            VendorUnique = new short[3];
            Reserved2 = new byte[382];
            FirmwareRevision = new char[8];
            SerialNumber = new char[20];
            ModelNumber = new char[40];
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    private class SENDCMDOUTPARAMS
    {
        public int BufferSize;
        public DRIVERSTATUS Status;
        public IDSECTOR IDS;
        public SENDCMDOUTPARAMS()
        {
            Status = new DRIVERSTATUS();
            IDS = new IDSECTOR();
        }
    }

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern int CloseHandle(int hObject);

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern int CreateFile(
                string lpFileName,
                uint dwDesiredAccess,
                int dwShareMode,
                int lpSecurityAttributes,
                int dwCreationDisposition,
                int dwFlagsAndAttributes,
                int hTemplateFile
            );

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern int DeviceIoControl(
            int hDevice,
            int dwIoControlCode,
            [In(), Out()] SENDCMDINPARAMS lpInBuffer,
            int lpInBufferSize,
            [In(), Out()] SENDCMDOUTPARAMS lpOutBuffer,
            int lpOutBufferSize,
            ref int lpBytesReturned,
            int lpOverlapped
        );

    private string SwapChars(char[] chars)
    {
        for (int i = 0; i <= chars.Length - 2; i += 2)
        {
            char t;
            t = chars[i];
            chars[i] = chars[i + 1];
            chars[i + 1] = t;
        }
        string s = new string(chars);
        return s;
    }


     private void button1_Click(object sender, System.EventArgs e)
    {

        string serialNumber = " ", model = " ", firmware = " ";
        bool result;
        DriveTypes driveType = DriveTypes.Unknown;
        int handle, returnSize = 0;
        int driveNumber = 0;
        SENDCMDINPARAMS sci = new SENDCMDINPARAMS();
        SENDCMDOUTPARAMS sco = new SENDCMDOUTPARAMS();

        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            //               \\.\PhysicalDrive0    Opens the first physical drive.
            //               \\.\PhysicalDrive2    Opens the third physical drive.
            // see more info on http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
            handle = CreateFile("\\\\.\\PhysicalDrive" + "0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
        else // for win'9x
            handle = CreateFile("\\\\.\\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0);
        if (handle != INVALID_HANDLE_VALUE)
        {
            sci.DriveNumber = (byte)driveNumber;
            sci.BufferSize = Marshal.SizeOf(sco);
            sci.DriveRegs.DriveHead = (byte)(0xA0 | driveNumber << 4);
            sci.DriveRegs.Command = 0xEC;
            sci.DriveRegs.SectorCount = 1;
            sci.DriveRegs.SectorNumber = 1;
            if (DeviceIoControl(handle, DFP_RECEIVE_DRIVE_DATA, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), ref returnSize, 0) != 0)
            {
                serialNumber = SwapChars(sco.IDS.SerialNumber);
                model = SwapChars(sco.IDS.ModelNumber);
                firmware = SwapChars(sco.IDS.FirmwareRevision);
            }

            textBox1.Text = serialNumber;
            textBox2.Text = model;
            textBox3.Text = firmware;
            if ((sco.IDS.GenConfig & 0x80) == 0x40)
                driveType = DriveTypes.Removable;
            else if ((sco.IDS.GenConfig & 0x40) == 0x40)
                driveType = DriveTypes.Fixed;
            else
                driveType = DriveTypes.Unknown;
            CloseHandle(handle);
            textBox4.Text = DriveStrings[(int)driveType];
        }
    }


Answer 3:

一个完全工作液可以在这里找到:

http://www.codeproject.com/Articles/16941/Get-Physical-HDD-Serial-Number-without-WMI

[编辑]对不起,错过了这个参考已经提交。



Answer 4:

您可以使用:

GetVolumeInformation

Win32 API函数来获取此信息,如果你必须避免WMI。 所链接的页面提供了完整的API函数声明的签名(在VB和C#)与示例代码。



Answer 5:

你最好的选择是Windows API的,

我做了一个简单的搜索和得到这个

通用处理器信息 ,并阅读这篇文章



Answer 6:

我用我的项目的硬盘固件轨道。 我编程的MDI形式背后寻找硬盘固件号,那么它的循环上的公司电脑硬盘中所有提供的数字,如果它匹配任何在这些提供硬盘固件号,然后运行applicatoin并加载MDI窗体其他明智的提供消息“应用程序不临时用户在本机上,请致电阿夫里迪先生对00923339176357.注册这个应用我的电子邮件地址是munawar_s_afridi@yahoo.com。我会送取如何将程序professionaly硬盘固件编号以及如何完整的源代码通过illegaly使用你的应用程序阻止他人。

你应立即指定所有公司电脑硬盘的固件,让应用程序先挑硬盘固件号,将其存储在一个变量,然后通过一个挑这个变量的值(一个字符串值)和环路它一个一个可爱的东西通过使用OR逻辑运算各HDD号码。 如果它发现里面设有变量值的公司的HDD号码的匹配数目,则应用程序应该加载主要形式(MDI)其他明智通知注册的用户。

示例代码将使用VB6提供。 您可以轻松地只是了解如何调用非托管代码中的托管.NET应用程序PROGRAMM后来在vb.net。 在这种情况下,一个.dll。



Answer 7:

从服务器资源管理器中使用VS WMI

  • 打开服务器资源管理器
  • 展开你的机器
  • 展开管理类
  • 展开磁盘卷
  • 选择驱动器
  • 其拖动到窗体上看到所生成的代码
  • 访问代码VolumeSerialNumber财产


文章来源: How can I find the Harddisk Device Serial Number without using the WMI in .NET?