c#怎么网卡MAC
不通过ManagementClass方式,这种方式很容易通过注册表被改掉。。
要低层的。
[解决办法]
c#中运行命令行截取输出流的例子
说明:经常有朋友问如何在c#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在java中实现过,由于在c#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在winform中ping一个网站,并且将ping的结果显示在一个
http://blog.csdn.net/zhoufoxcn/archive/2007/07/07/1682130.aspx
你将这个改改就能用。
tbResult.Text = ""; ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe start.Arguments = txtCommand.Text;//设置命令参数 start.CreateNoWindow = true;//不显示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p=Process.Start(start); StreamReader reader = p.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 while (!reader.EndOfStream) { tbResult.AppendText(line+" "); line = reader.ReadLine(); } p.WaitForExit();//等待程序执行完退出进程 p.Close();//关闭进程 reader.Close();//关闭流
[解决办法]
[DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); [DllImport("Ws2_32.dll")] private static extern Int32 inet_addr(string ip); public string getRemoteMac(string localIP, string remoteIP) { Int32 ldest= inet_addr(remoteIP); Int32 lhost= inet_addr(localIP); try { Int64 macinfo = new Int64(); Int32 len = 6; int res = SendARP(ldest,0, ref macinfo, ref len); return Convert.ToString(macinfo,16); } catch(...) { ... }}
[解决办法]
完整代码:(remoteIP不能是127.0.0.1,可以是localhost或者实际分配的IP)
[DllImport("Iphlpapi.dll")]private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);[DllImport("Ws2_32.dll")]private static extern Int32 inet_addr(string ip);public string getRemoteMac(string remoteIP){ StringBuilder macAddress = new StringBuilder(); try { Int32 remote = inet_addr(remoteIP); Int64 macInfo = new Int64(); Int32 length = 6; SendARP(remote, 0, ref macInfo, ref length); string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper(); int x = 12; for (int i = 0; i < 6; i++) { if (i == 5) { macAddress.Append(temp.Substring(x - 2, 2)); } else { macAddress.Append(temp.Substring(x - 2, 2) + "-"); } x -= 2; } return macAddress.ToString(); } catch { return macAddress.ToString(); }}
[解决办法]
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel.Design;
using Microsoft.Win32;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
/// <summary>
/// 读取网卡mac地址
/// </summary>
namespace MyUtility
{
public class MacAddr
{
[DllImport("Iphlpapi.dll")]
public static extern uint GetAdaptersAddresses(uint Family, uint flags, IntPtr Reserved,
IntPtr PAdaptersAddresses, ref uint pOutBufLen);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class IP_Adapter_Addresses
{
public uint Length;
public uint IfIndex;
public IntPtr Next;
public IntPtr AdapterName;
public IntPtr FirstUnicastAddress;
public IntPtr FirstAnycastAddress;
public IntPtr FirstMulticastAddress;
public IntPtr FirstDnsServerAddress;
public IntPtr DnsSuffix;
public IntPtr Description;
public IntPtr FriendlyName;
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 8)]
public Byte[] PhysicalAddress;
public uint PhysicalAddressLength;
public uint flags;
public uint Mtu;
public uint IfType;
public uint OperStatus;
public uint Ipv6IfIndex;
public uint ZoneIndices;
public IntPtr FirstPrefix;
}
private string P_NetIP = "";
public MacAddr()
{
}
public ArrayList GetMacAddressList()
{
ArrayList NetAdapterList = new ArrayList();
IntPtr PAdaptersAddresses = new IntPtr();
bool AdapterFound = false;
uint pOutLen = 100;
PAdaptersAddresses = Marshal.AllocHGlobal(100);
uint ret =
GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);
// if 111 error, use
if (ret == 111)
{
Marshal.FreeHGlobal(PAdaptersAddresses);
PAdaptersAddresses = Marshal.AllocHGlobal((int)pOutLen);
ret = GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);
}
IP_Adapter_Addresses adds = new IP_Adapter_Addresses();
IntPtr pTemp = PAdaptersAddresses;
IntPtr pTempIP = new IntPtr();
while (pTemp != (IntPtr)0)
{
Marshal.PtrToStructure(pTemp, adds);
string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
string tmpString = string.Empty;
for (int i = 0; i < 6; i++)
{
tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);
if (i < 5)
{
tmpString += ":";
}
}
RegistryKey theLocalMachine = Registry.LocalMachine;
RegistryKey theSystem
= theLocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces");
RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName);
if (theInterfaceKey != null)
{
string DhcpIPAddress = (string)theInterfaceKey.GetValue("DhcpIPAddress");
// system is using DHCP
if (DhcpIPAddress != null)
{
string tArray = (string)
theInterfaceKey.GetValue("DhcpIPAddress", theInterfaceKey);
string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();
string NetIP = "IP Address: " + tArray ;
string NetMac = "MAC Address: " + tmpString ;
NetAdapterList.Add(NetWorkAdapterName + "<>" + NetIP + "<>" + NetMac);
AdapterFound = true;
}
else
{
string[] tArray = (string[])
theInterfaceKey.GetValue("IPAddress", theInterfaceKey);
string NetWorkAdapterName = "Network adapter: " + FriendlyName.ToString();
P_NetIP = "";
for (int Interface = 0; Interface < tArray.Length; Interface++)
{
P_NetIP += "IP Address: " + tArray[Interface];
AdapterFound = true;
}
string NetMac = "MAC Address: " + tmpString;
NetAdapterList.Add(NetWorkAdapterName + "<>" + P_NetIP + "<>" + NetMac);
}
}
pTemp = adds.Next;
}
if (AdapterFound != true)
{
//AdapterInfoTextBox.Text += "No network adapters configured/present\r\n";
}
return NetAdapterList;
}
}
}