日期:2014-05-20  浏览次数:21030 次

c#怎么网卡MAC
不通过ManagementClass方式,这种方式很容易通过注册表被改掉。。

要低层的。




------解决方案--------------------
c#中运行命令行截取输出流的例子
说明:经常有朋友问如何在c#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在java中实现过,由于在c#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在winform中ping一个网站,并且将ping的结果显示在一个
http://blog.csdn.net/zhoufoxcn/archive/2007/07/07/1682130.aspx

你将这个改改就能用。
C# code

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();//关闭流

------解决方案--------------------
C# code

[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)

C# code

[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);