快捷搜索:

C#如何通过IP获取局域网电脑信息

【IT168技术文档】 
    前提是需要有指定机器的管理员账号。账号可以放到服务器的XML配置文件里面。客户端程序去调用。

 C# code

 /// <summary>

 /// 获取业务系统部署服务器的硬盘码|MAC|其他

 /// </summary>

 /// <returns>返回硬盘码</returns>

 public static string GetHardDiskCode()

 {

 String strHardDiskID = null;

 try

 {

 string strip;

 strip = Clscode.ServerYW_IP;

 System.Management.ConnectionOptions Conn = new ConnectionOptions();

 Conn.Username = Clscode.ServerYW_UID;  //设定用户的口令

 Conn.Password = Clscode.ServerYW_PWD;  //设定用于执行WMI操作的范围

 System.Management.ManagementScope Ms = new ManagementScope("\\\\" + strip + "\\root\\cimv2", Conn);

 strHardDiskID = GetMacAddress(strip).ToLower();

 if (string.IsNullOrEmpty(strHardDiskID))

 {

 ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PhysicalMedia");   //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合

 ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Ms, Query);   //异步调用WMI查询

 foreach (ManagementObject mo in Searcher.Get())

 {

 if (mo["SerialNumber"] != null)

 {

 strHardDiskID = mo["SerialNumber"].ToString().Trim().ToLower();

 break;

 }

 }

 }

 if (string.IsNullOrEmpty(strHardDiskID))

 {

 ObjectQuery OQuery = new ObjectQuery("SELECT * FROM Win32_BaseBoard");   //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合

 ManagementObjectSearcher OSearcher = new ManagementObjectSearcher(Ms, OQuery);   //异步调用WMI查询

 foreach (ManagementObject mo in OSearcher.Get())

 {

 if (mo["SerialNumber"] != null)

 {

 strHardDiskID = mo["SerialNumber"].ToString().Trim().ToLower();

 break;

 }

 }

 if (string.IsNullOrEmpty(strHardDiskID))

 {

 strHardDiskID = "hxrt2007";

 }

 }

 return strHardDiskID;

 }

 catch (Exception ex)

 {

 try

 {

 System.Management.ManagementScope Ms = new ManagementScope();

 ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PhysicalMedia");   //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合

 ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Ms, Query);   //异步调用WMI查询

 foreach (ManagementObject mo in Searcher.Get())

 {

 if (mo["SerialNumber"] != null)

 {

 strHardDiskID = mo["SerialNumber"].ToString().Trim().ToLower();

 break;

 }

 }

 if (string.IsNullOrEmpty(strHardDiskID))

 {

 ObjectQuery OQuery = new ObjectQuery("SELECT * FROM Win32_BaseBoard");   //WQL语句,设定的WMI查询内容和WMI的操作范围,检索WMI对象集合

 ManagementObjectSearcher OSearcher = new ManagementObjectSearcher(Ms, OQuery);   //异步调用WMI查询

 foreach (ManagementObject mo in OSearcher.Get())

 {

 if (mo["SerialNumber"] != null)

 {

 strHardDiskID = mo["SerialNumber"].ToString().Trim().ToLower();

 break;

 }

 }

 if (string.IsNullOrEmpty(strHardDiskID))

 {

 strHardDiskID = "hxrt2007";

 }

 }

 return strHardDiskID;

 }

 catch (Exception err)

 {

 Clscommonuse.WriteError("FrameModel", "GetHardDiskCode", ex.Source, ex.Message);

 return "hxrt2007";

您可能还会对下面的文章感兴趣: