日期:2014-05-20 浏览次数:21030 次
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);