|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 liucqa 于 2012-11-21 14:34 编辑
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Management;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Collections;
- //在引用中添加.NET选项卡中的System.Management类库
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("计算机名:" + HInfo.MyHardwareInfo.GetHostName()); //static定义的方法,可以直接使用,耗资源
- HInfo.MyHardwareInfo temp = new HInfo.MyHardwareInfo();
- //Console.WriteLine("硬盘 ID:" + temp.GetHardDiskID()); //非static定义的方法,只能先new之后,再引用
- //Console.WriteLine("Mac地址:" + temp.GetMacAddress()); //都需要插上网线
- //Console.WriteLine("CPU ID:" + temp.GetCpuID());
- Console.WriteLine("主板 ID:" + temp.GetMainBoard());
- Console.WriteLine("硬盘 ID:" + temp.GetHDSN());
- Console.WriteLine("BIOS ID:" + temp.GetBiosSN());
- //Console.WriteLine("CPU Info: " + temp.GetCPUInfo());
- //Console.WriteLine("Mac地址 Info:" + GetMacAddress.MAC.GetMACIP());
- //Console.WriteLine("Local IP:" + GetMacAddress.getIP.getLocalIP());
- //Console.WriteLine("Local Mac:" + GetMacAddress.getIP.getLocalMac());
- ArrayList allNics = GetMacAddress.MACInfo.GetAllNic();
- foreach (object Nicid in allNics)
- {
- Console.WriteLine("MacInfo:" + GetMacAddress.MACInfo.GetNicAddress(Nicid.ToString()).Trim());
- }
- Console.ReadKey();
- }
- }
- }
- namespace HInfo
- {
- public class MyHardwareInfo
- {
- //取机器名
- public static string GetHostName()
- {
- return System.Net.Dns.GetHostName();
- }
- //取CPU编号
- public String GetCpuID()
- {
- try
- {
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- String strCpuID = null;
- foreach (ManagementObject mo in moc)
- {
- strCpuID = mo.Properties["ProcessorId"].Value.ToString();
- break;
- }
- return strCpuID;
- }
- catch
- {
- return "";
- }
- }//end
- //获取主板编号
- public String GetMainBoard()
- {
- ManagementClass mc = new ManagementClass("Win32_BaseBoard");
- ManagementObjectCollection moc = mc.GetInstances();
- string strID = null ;
- foreach( ManagementObject mo in moc )
- {
- strID = mo.Properties["SerialNumber"].Value.ToString();
- break;
- }
- return strID ;
- }//End
- //获取主板编号
- public String GetBiosSN()
- {
- ManagementClass mc = new ManagementClass("Win32_BIOS");
- ManagementObjectCollection moc = mc.GetInstances();
- string strID = null ;
- foreach( ManagementObject mo in moc )
- {
- strID = mo.Properties["SerialNumber"].Value.ToString();
- break;
- }
- return strID;
- }//End
- //获取CPU的全部信息
- public String GetCPUInfo()
- {
- ManagementClass mc = new ManagementClass("Win32_Processor");
- ManagementObjectCollection moc = mc.GetInstances();
- string temp = null;
- foreach (ManagementObject mo in moc)
- {
- temp += "\r\n============CUP信息============";
- foreach (PropertyData pd in mo.Properties)
- {
- temp += "\r\n" + pd.Name + "\t";
- if (pd.Value != null)
- {
- temp += pd.Value.ToString();
- }
- }
- temp += "\r\n\r\n========================";
- }
- return temp;
- }//End
- //获取硬盘编号
- public String GetHDSN()
- {
- ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
- //网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
- ManagementObjectCollection moc = mc.GetInstances();
- string strID = null;
- foreach (ManagementObject mo in moc)
- {
- strID = mo.Properties["SerialNumber"].Value.ToString().Trim();
- break;
- }
- return strID;
- }//End
- //取第一块硬盘编号
- public String GetHardDiskID()
- {
- try
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
- String strHardDiskID = null;
- foreach (ManagementObject mo in searcher.Get())
- {
- strHardDiskID = mo["SerialNumber"].ToString().Trim();
- break;
- }
- return strHardDiskID;
- }
- catch
- {
- return "";
- }
- }//end
- public enum NCBCONST
- {
- NCBNAMSZ = 16, /* absolute length of a net name */
- MAX_LANA = 254, /* lana's in range 0 to MAX_LANA inclusive */
- NCBENUM = 0x37, /* NCB ENUMERATE LANA NUMBERS */
- NRC_GOODRET = 0x00, /* good return */
- NCBRESET = 0x32, /* NCB RESET */
- NCBASTAT = 0x33, /* NCB ADAPTER STATUS */
- NUM_NAMEBUF = 30, /* Number of NAME's BUFFER */
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct ADAPTER_STATUS
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
- public byte[] adapter_address;
- public byte rev_major;
- public byte reserved0;
- public byte adapter_type;
- public byte rev_minor;
- public ushort duration;
- public ushort frmr_recv;
- public ushort frmr_xmit;
- public ushort iframe_recv_err;
- public ushort xmit_aborts;
- public uint xmit_success;
- public uint recv_success;
- public ushort iframe_xmit_err;
- public ushort recv_buff_unavail;
- public ushort t1_timeouts;
- public ushort ti_timeouts;
- public uint reserved1;
- public ushort free_ncbs;
- public ushort max_cfg_ncbs;
- public ushort max_ncbs;
- public ushort xmit_buf_unavail;
- public ushort max_dgram_size;
- public ushort pending_sess;
- public ushort max_cfg_sess;
- public ushort max_sess;
- public ushort max_sess_pkt_size;
- public ushort name_count;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct NAME_BUFFER
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
- public byte[] name;
- public byte name_num;
- public byte name_flags;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct NCB
- {
- public byte ncb_command;
- public byte ncb_retcode;
- public byte ncb_lsn;
- public byte ncb_num;
- public IntPtr ncb_buffer;
- public ushort ncb_length;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
- public byte[] ncb_callname;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
- public byte[] ncb_name;
- public byte ncb_rto;
- public byte ncb_sto;
- public IntPtr ncb_post;
- public byte ncb_lana_num;
- public byte ncb_cmd_cplt;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
- public byte[] ncb_reserve;
- public IntPtr ncb_event;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct LANA_ENUM
- {
- public byte length;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]
- public byte[] lana;
- }
- [StructLayout(LayoutKind.Auto)]
- public struct ASTAT
- {
- public ADAPTER_STATUS adapt;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]
- public NAME_BUFFER[] NameBuff;
- }
- public class Win32API
- {
- [DllImport("NETAPI32.DLL")]
- public static extern char Netbios(ref NCB ncb);
- }
- //取网卡mac
- public string GetMacAddress()
- {
- string addr = "";
- try
- {
- int cb;
- ASTAT adapter;
- NCB Ncb = new NCB();
- char uRetCode;
- LANA_ENUM lenum;
- Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
- cb = Marshal.SizeOf(typeof(LANA_ENUM));
- Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
- Ncb.ncb_length = (ushort)cb;
- uRetCode = Win32API.Netbios(ref Ncb);
- lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
- Marshal.FreeHGlobal(Ncb.ncb_buffer);
- if (uRetCode != (short)NCBCONST.NRC_GOODRET)
- return "";
- for (int i = 0; i < lenum.length; i++)
- {
- Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
- Ncb.ncb_lana_num = lenum.lana[i];
- uRetCode = Win32API.Netbios(ref Ncb);
- if (uRetCode != (short)NCBCONST.NRC_GOODRET)
- return "";
- Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
- Ncb.ncb_lana_num = lenum.lana[i];
- Ncb.ncb_callname[0] = (byte)'*';
- cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;
- Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
- Ncb.ncb_length = (ushort)cb;
- uRetCode = Win32API.Netbios(ref Ncb);
- adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
- Marshal.FreeHGlobal(Ncb.ncb_buffer);
- if (uRetCode == (short)NCBCONST.NRC_GOODRET)
- {
- if (i > 0)
- addr += ":";
- addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
- adapter.adapt.adapter_address[0],
- adapter.adapt.adapter_address[1],
- adapter.adapt.adapter_address[2],
- adapter.adapt.adapter_address[3],
- adapter.adapt.adapter_address[4],
- adapter.adapt.adapter_address[5]);
- }
- }
- }
- catch
- {
- }
- return addr.Replace(' ', '0');
- }
- }
- }
- namespace GetMacAddress
- {
- class MAC
- {
- [STAThread]
- public static string GetMACIP()
- {
- string mac = "";
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if (mo["IPEnabled"].ToString() == "True")
- {
- mac = mo["MacAddress"].ToString();
- }
- }
- return mac.ToString();
- }
- }
-
- class MACInfo
- {
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool DeviceIoControl(
- IntPtr HDevice,
- uint dwIoControlCode,
- IntPtr lpInBuffer,
- uint nInBufferSize,
- IntPtr lpOutBuffer,
- uint nOutBufferSize,
- ref uint lpBytesReturned,
- IntPtr lpOverlapped);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr CreateFile(
- string lpFileName,
- uint dwDesiredAccess,
- uint dwShareMode,
- IntPtr lpSecurityAttributes,
- uint dwCreationDisposition,
- uint dwFlagsAndAttributes,
- IntPtr hTemplateFile);
- public static string GetNicAddress(string NicId)
- {
- System.IntPtr hDevice = CreateFile("\\\\.\" + NicId,
- 0x80000000 | 0x40000000,
- 0,
- IntPtr.Zero,
- 3,
- 4,
- IntPtr.Zero
- );
- if (hDevice.ToInt32() == -1)
- {
- return null;
- }
- uint Len = 0;
- IntPtr Buffer = Marshal.AllocHGlobal(256);
- Marshal.WriteInt32(Buffer, 0x01010101);
- if (!DeviceIoControl(hDevice,
- 0x170002,
- Buffer,
- 4,
- Buffer,
- 256,
- ref Len,
- IntPtr.Zero))
- {
- Marshal.FreeHGlobal(Buffer);
- CloseHandle(hDevice);
- return null;
- }
- byte[] macBytes = new byte[6];
- Marshal.Copy(Buffer, macBytes, 0, 6);
- Marshal.FreeHGlobal(Buffer);
- CloseHandle(hDevice);
- return new System.Net.NetworkInformation.PhysicalAddress(macBytes).ToString();
- }
- //以下函数获取本机所有的以太网卡的ID
- public static ArrayList GetAllNic()
- {
- NetworkInterface[] Nics = NetworkInterface.GetAllNetworkInterfaces();
- System.Collections.ArrayList EtherNics = new ArrayList(20);
- foreach (NetworkInterface nic in Nics)
- {
- if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet && !nic.Name.Contains("Bluetooth")) //Wireless80211,无线
- //if (nic.NetworkInterfaceType.ToString().Equals("Ethernet")) //会把蓝牙找出来
- {
- EtherNics.Add(nic.Id);
- }
- }
- return EtherNics;
- }
- }
- }
复制代码
C#识别机器特征码,作废的代码不少,请自行删除。
|
|