9

I'm implementing in C# a discovery protocol (DNS-SD). I've a small problem: when my program is running, I need to detect when a new connection is established (network cable plugged or wifi joined), to announce myself on those network too.

But how to detect this? I've seen some code to get the current state of a network card, but no event, and I want to avoid to make some polling on the network cards states.

slugster
  • 49,403
  • 14
  • 95
  • 145
J4N
  • 19,480
  • 39
  • 187
  • 340
  • 1
    Check this out: http://stackoverflow.com/questions/1387650/how-to-get-an-event-whenever-the-network-cable-is-connected-or-disconnected – Jason Evans Feb 26 '13 at 09:45

1 Answers1

4

You can use the GetIstNetworkAvailable-method to check if there is a network connection at all. Whenever it changes the NetworkAvailabilityChanged event get's fired. For more information see this question.

If you want to track the availability of different NetworkInterfaces here is an sample on how to do this.

Community
  • 1
  • 1
Carsten
  • 11,287
  • 7
  • 39
  • 62
  • I may have misunderstood but I believe the OP needs more information than that - eg Connect LAN (event raised), Connect Wifi (silent?) disconnect LAN (silent?), disconnect Wifi (event). – Basic Feb 26 '13 at 10:38
  • 1
    The NetworkAvailabilityChanged-Event should be raised whenever one of the available networkinterfaces availability changes. You can then test which networkinterfaces are available. I've updated the answer ;) – Carsten Feb 26 '13 at 10:49
  • 1
    Hi, I think that with your last example, I could make what I need – J4N Feb 26 '13 at 10:49
  • 2
    @Aschratt +1 nice addition – Basic Feb 26 '13 at 10:56