C# Get network adapter info.

现实中,我们的机器可以有多块网卡(network adapter),
每块网卡只能有一个MAC Address,
但一块网卡又可以绑定多个IP Address。

现本机安装有3块网卡(其中后两块是虚的假卡),
第一块(物理存在的真卡)绑定了三个IP地址,
具体情况如下:

MAC Address : 00:05:5D:72:B6:53
      IP Address : 192.168.0.47
      IP Address : 192.168.0.106
      IP Address : 192.168.0.220

MAC Address : 00:50:56:C0:00:01
      IP Address : 192.168.204.1

MAC Address : 00:50:56:C0:00:08
      IP Address : 192.168.74.1

下面的程序将获取以上内容:

None.gif using  System;
None.gif
using  System.Management; // It is need import in addition.
None.gif
using  System.Collections;
None.gif
None.gif
namespace  PublicBill.GetNetworkAdapter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class GetNetworkAdapter
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NetworkAdapter[] networkAdapters 
= (new GetNetworkAdapter()).GetNetworkAdapters();
InBlock.gif            
foreach(NetworkAdapter networkAdapter in networkAdapters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"MAC Address : " + networkAdapter.MAC);
InBlock.gif                
foreach(string ips in networkAdapter.IP)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
" IP Address : " + ips);
ExpandedSubBlockEnd.gif                }

InBlock.gif                Console.WriteLine();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get network adapters.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>return Network Adapter array.</returns>

InBlock.gif        public NetworkAdapter[] GetNetworkAdapters()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList macs 
= new ArrayList();
InBlock.gif
InBlock.gif            ManagementClass mc 
= new ManagementClass("Win32_NetworkAdapterConfiguration");
InBlock.gif            ManagementObjectCollection moc 
= mc.GetInstances();
InBlock.gif            
foreach(ManagementObject mo in moc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(Convert.ToBoolean(mo["ipEnabled"]) == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string mac = mo["MacAddress"].ToString();
InBlock.gif                    
string[] ips = (string[])mo["IPAddress"];
InBlock.gif
InBlock.gif                    NetworkAdapter networkAdapter 
= new NetworkAdapter();
InBlock.gif                    networkAdapter.MAC 
= mac;
InBlock.gif                    networkAdapter.IP 
= ips;
InBlock.gif
InBlock.gif                    macs.Add(networkAdapter);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return (NetworkAdapter[])macs.ToArray(typeof(NetworkAdapter));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Simulation a Network Adapter, 
InBlock.gif    
/// each one contains one mac address 
InBlock.gif    
/// and two or more ip address.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class NetworkAdapter
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string mac;
InBlock.gif        
private string[] ip;
InBlock.gif
InBlock.gif        
public string MAC
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn mac; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ mac = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string[] IP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ip; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ip = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

以下是程序运行后的结果:
CSharpGetNetworkAdapter(051009).JPG 

有关“Win32_NetworkAdapterConfiguration”,请参照
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_networkadapterconfiguration.asp

转载于:https://www.cnblogs.com/publicbill/archive/2005/10/09/250984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值