0

Can SharpPcap auto choose Ethernet for capturing packets?

var device = CaptureDeviceList.Instance[3];//I don't want to hard-code it.

Should I use "if, else" or "for-loop" to check it?

Sam1916
  • 11
  • 4

1 Answers1

0

Use Foreach-loop:

var devices = CaptureDeviceList.Instance;
foreach (var i in devices)
    {
        string Local_eth = "Ethernet";
        if (i.Description.Contains(Local_eth))
        {
            codes;
        }
Sam1916
  • 11
  • 4
  • Or, better, use LINQ: `var device = CaptureDeviceList.Instance.FirstOrDefault(dev => dev.Description.Contains("Ethernet"));` (You should check whether `dev` is null or not before using it though.) – Jon Skeet Dec 15 '22 at 04:59
  • Wow, it works perfectly, thanks so much. I'm new at C#(I'm better at python). This is my first project with C#(Create Windows Service). – Sam1916 Dec 15 '22 at 05:42