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.Name = "txtMac ";
this.txtMac.ReadOnly = true;
this.txtMac.Size = new System.Drawing.Size(176, 21);
this.txtMac.TabIndex = 3;
this.txtMac.Text = " ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 34);
this.label2.Name = "label2 ";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 2;
this.label2.Text = "MAC: ";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnQuery
//
this.btnQuery.Location = new System.Drawing.Point(296, 19);
this.btnQuery.Name = "btnQuery ";
this.btnQuery.TabIndex = 4;
this.btnQuery.Text = "查 询 ";
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
//
// QGetRemoteMac
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 61);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnQuery,
this.txtMac,
this.label2,
this.txtName,
this.label1});
this.Name = "QGetRemoteMac ";
this.Text = "QGetRemoteMac ";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new QGetRemoteMac());
}
private void btnQuery_Click(object sender, System.EventArgs e)
{
System.Net.IPAddress addr,addrRemote;
// 获得本机局域网IP地址
addr = new System.Net.IPAddress ( Dns.GetHostByName (
Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
//获得远程计算机的IP地址
IPHostEntry remote;
try
{
remote= Dns.GetHostByName(txtName.Text);
addrRemote=new System.Net.IPAddress ( remote.AddressList[0].Address) ;
string localIP,remoteIP;
localIP=addr.ToString();
remoteIP=addrRemote.ToString();
Int64 macinfo=getRemoteMAC( localIP,remoteIP );
byte [] temp=new byte[6];
for(int i=0;i <6;i++)
{
temp[i]=Convert.ToByte(macinfo%256);
macinfo/=256;
}
txtMac.Text=String.Format( "{0:x}-{1:x}-{2:x}-{3:x}-{4:x}-{5:x} ",
temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
}
catch(Exception ex)
{
MessageBox.Show( "找不到主机! ");
}
}
}
}
[解决办法]
privatestring[]GetMoc()
{
string[]str=newstring[3];
ManagementClassmcCpu=newManagementClass( "win32_Processor ");
ManagementObjectCollectionmocCpu=mcCpu.GetInstances();
foreach(ManagementObjectminmocCpu)
{
str[0]=m[ "ProcessorId "].ToString();
}
ManagementClassmcHD=newManagementClass( "win32_logicaldisk ");
ManagementObjectCollectionmocHD=mcHD.GetInstances();
foreach(ManagementObjectminmocHD)
{
if(m[ "DeviceID "].ToString()== "C: ")
{
str[1]=m[ "VolumeSerialNumber "].ToString();
break;
}
}
ManagementClassmcMAC=newManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollectionmocMAC=mcMAC.GetInstances();
foreach(ManagementObjectminmocMAC)
{
if((bool)m[ "IPEnabled "])
{
str[2]=m[ "MacAddress "].ToString();
break;
}
}
returnstr;
}
[解决办法]
为了点分,看看你们贴的代码.精灵鼠的是最可恨的
[解决办法]
ProcessStartInfo psi = new ProcessStartInfo( "ipconfig ", "/all ");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
Console.WriteLine(Regex.Match(p.StandardOutput.ReadToEnd(), @ "[0-9A-F]{2}(-[0-9A-F]{2}){5} "));