We can use Process to call cmd to execute this command, and then convert the result into an object in c#.
class Program
{
static void Main(string[] args)
{
ArpUtil arpHelper = new ArpUtil();
List<ArpItem> arpEntities = arpHelper.GetArpResult();
foreach (var item in arpEntities)
{
Console.WriteLine(item.Ip+"\t"+item.MacAddress+"\t"+item.Type);
}
Console.ReadLine();
}
}
public class ArpItem
{
public string Ip { get; set; }
public string MacAddress { get; set; }
public string Type { get; set; }
}
public class ArpUtil
{
public List<ArpItem> GetArpResult()
{
using (Process process = Process.Start(new ProcessStartInfo("arp", "-a")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
}))
{
var output = process.StandardOutput.ReadToEnd();
return ParseArpResult(output);
}
}
private List<ArpItem> ParseArpResult(string output)
{
var lines = output.Split('\n');
var result = from line in lines
let item = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
where item.Count() == 4
select new ArpItem()
{
Ip = item[0],
MacAddress = item[1],
Type = item[2]
};
return result.ToList();
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.