开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 7751|回复: 17

[教程] C#取得本地电脑硬件信息

[复制链接]

0

精华

5113

贡献

5368

赞扬

管理员

帖子
1156
软币
21143
在线时间
4334 小时
注册时间
2013-6-7

黄马甲

发表于 2013-6-10 23:10:15 | 显示全部楼层 |阅读模式
using System.Management;

//取CPU号
  1. public string GetCpuID()
  2. {
  3. try
  4. {
  5.   ManagementClass mc = new ManagementClass("Win32_Processor");
  6.   ManagementObjectCollection moc = mc.GetInstances();
  7.   string strCpuID = null ;
  8.   foreach( ManagementObject mo in moc )
  9.   {
  10.    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
  11.    break;
  12.   }
  13.   return strCpuID;
  14. }
  15. catch
  16. {
  17.   return "";
  18. }
  19. }
复制代码
//取第一块硬盘号
  1. public string GetHardDiskID()
  2. {
  3. try
  4. {
  5.   ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * FROM Win32_PhysicalMedia");
  6.   string strHardDiskID = null ;
  7.   foreach(ManagementObject mo in searcher.Get())
  8.   {   
  9.    strHardDiskID = mo["ModuleNumber"].ToString().Trim();
  10.    if(strHardDiskID != "")
  11.    {
  12.     break;
  13.    }
  14.   }
  15.   return strHardDiskID ;
  16. }
  17. catch
  18. {
  19.   return "";
  20. }
  21. }
复制代码
//取第一块网卡号
  1. public string GetNetworkID()
  2. {
  3. try
  4. {
  5.   ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * FROM Win32_NetworkAdapterConfiguration");
  6.   string strNetworkID = null ;
  7.   foreach(ManagementObject mo in searcher.Get())
  8.   {   
  9.    strNetworkID = mo["MACAddress"].ToString().Trim();
  10.    if(strNetworkID != "")
  11.    {
  12.     break;         
  13.    }
  14.   }
  15.   return strNetworkID ;
  16. }
  17. catch
  18. {
  19.   return "";
  20. }
  21. }
复制代码
//取串口
  1. public int getport(int portnum)
  2. {
  3. //链接服务器
  4. DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
  5. //检测是否已存在相同串口的站点,并获取PORT值
  6. foreach(DirectoryEntry e in root.Children)
  7. {
  8.   if(e.SchemaClassName == "IIsWebServer")
  9.   {
  10.    if(e.Properties["ServerBindings"].Value != null)
  11.    {
  12.     int strstart = e.Properties["ServerBindings"].Value.ToString().IndexOf(":",0);
  13.     int strend = e.Properties["ServerBindings"].Value.ToString().IndexOf(":",strstart+1);
  14.     string thisport = e.Properties["ServerBindings"].Value.ToString().Substring((strstart+1),(strend-strstart-1));
  15.     if(thisport == portnum.ToString())
  16.     {
  17.      portnum++;
  18.      portnum = getport(portnum);
  19.      break;
  20.     }
  21.    }
  22.   }
  23. }
  24. return portnum;
  25. }
复制代码

评分

参与人数 2赞扬 +2 收起 理由
微笑等待 + 1 Thanks
pbuilder + 1 感谢分享

查看全部评分

回复

使用道具 举报

0

精华

0

贡献

15

赞扬

帖子
15
软币
130
在线时间
4 小时
注册时间
2013-6-11
发表于 2013-6-12 06:16:13 | 显示全部楼层
This one is great!
回复

使用道具 举报

0

精华

0

贡献

15

赞扬

帖子
15
软币
130
在线时间
4 小时
注册时间
2013-6-11
发表于 2013-6-12 06:18:03 | 显示全部楼层
How about Motherboard and BIOS ID? These will complete the library :)
回复

使用道具 举报

0

精华

10

贡献

90

赞扬

帖子
31
软币
349
在线时间
21 小时
注册时间
2013-6-15
发表于 2013-7-3 08:36:40 | 显示全部楼层
谢谢,提供这样好的东西。
回复

使用道具 举报

0

精华

730

贡献

2924

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
413
软币
15869
在线时间
2986 小时
注册时间
2013-6-8

饮水勋章神经正常武林秘籍

发表于 2013-7-4 07:58:47 | 显示全部楼层
可以提取机器码
回复

使用道具 举报

0

精华

657

贡献

5063

赞扬

帖子
213
软币
21422
在线时间
1804 小时
注册时间
2013-6-9
发表于 2013-7-5 14:21:58 | 显示全部楼层
感谢楼主共享代码。
回复

使用道具 举报

0

精华

26

贡献

12

赞扬

帖子
74
软币
259
在线时间
17 小时
注册时间
2013-9-27
发表于 2013-10-15 13:42:09 | 显示全部楼层
好东西,收藏一个
回复

使用道具 举报

0

精华

102

贡献

15

赞扬

帖子
136
软币
2024
在线时间
288 小时
注册时间
2013-6-22
发表于 2013-10-17 14:56:23 | 显示全部楼层
不错,收藏下
回复

使用道具 举报

0

精华

103

贡献

18

赞扬

帖子
66
软币
1098
在线时间
116 小时
注册时间
2013-6-12
发表于 2013-10-23 06:11:59 | 显示全部楼层
正好有用。资源很不错。
回复

使用道具 举报

0

精华

495

贡献

245

赞扬

帖子
258
软币
2556
在线时间
138 小时
注册时间
2013-6-9
发表于 2013-10-23 17:05:28 | 显示全部楼层
C#取硬盘的可能会有重复,大家用的时候多测试下
回复

使用道具 举报

0

精华

3

贡献

0

赞扬

帖子
7
软币
68
在线时间
2 小时
注册时间
2013-10-28
发表于 2013-10-28 16:19:16 | 显示全部楼层
不错,收藏下
回复

使用道具 举报

0

精华

55

贡献

20

赞扬

帖子
138
软币
1187
在线时间
157 小时
注册时间
2013-10-16
发表于 2013-11-4 22:25:33 | 显示全部楼层
不错,一直在用这个代码,顶起。
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
17
软币
147
在线时间
8 小时
注册时间
2014-7-4
发表于 2014-7-8 08:42:07 | 显示全部楼层
很值得参考
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
17
软币
147
在线时间
8 小时
注册时间
2014-7-4
发表于 2014-7-8 08:47:30 | 显示全部楼层
能获取ID,好牛
回复

使用道具 举报

0

精华

1

贡献

6

赞扬

帖子
54
软币
332
在线时间
25 小时
注册时间
2014-8-22
发表于 2014-8-22 10:07:44 | 显示全部楼层
学习了,好代码
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
10
软币
80
在线时间
3 小时
注册时间
2014-9-24
发表于 2014-9-25 11:11:09 | 显示全部楼层
感谢分享,好帖收藏
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
62
软币
1202
在线时间
130 小时
注册时间
2013-12-9
发表于 2014-12-20 11:16:54 | 显示全部楼层
感谢楼主分享,很實用。
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-17 06:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表