日期:2014-05-19  浏览次数:20886 次

100分 c#取得网卡地址,请详细代码
100分   c#取得网卡地址,请详细代码

------解决方案--------------------
http://www.cnblogs.com/doll-net/archive/2007/03/07/667467.html
------解决方案--------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Net;

namespace QGetRemoteMac
{
/// <summary>
/// QGetRemoteMac 的摘要说明。
/// </summary>
public class QGetRemoteMac : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtMac;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnQuery;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

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

static private Int64 getRemoteMAC(string localIP, string remoteIP)
{
Int32 ldest= inet_addr(remoteIP); //目的地的ip
Int32 lhost= inet_addr(localIP); //本地服务器的ip

try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
MessageBox.Show( "Error:{0} ",err.Message);
}
return 0;
}


public QGetRemoteMac()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtMac = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnQuery = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1 ";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 0;
this.label1.Text = "主机名: ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(72, 8);
this.txtName.Name = "txtName ";
this.txtName.Size = new System.Drawing.Size(176, 21);
this.txtName.TabIndex = 1;
this.txtName.Text = " ";
//
// txtMac
//
this.txtMac.Location = new System.Drawing.Point(72, 35);
this.txtMac.N