question

Deadlock-0899 avatar image
0 Votes"
Deadlock-0899 asked TimonYang-MSFT answered

(windows app C#) need help scanning local network devices on windows 10

So when you type "arp - a" into a command prompt it gives a list of local devices and MAC address connected to my internet. I want to know how to turn That into a windows app form in C#

If anyone can help that would be nice

dotnet-csharpwindows-10-network
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.