Establish ethernet connection in C#

4.5k Views Asked by At

Can you tell me please how can I establish/tear down an existing ethernet (LAN) connection using C#?

In Windows UI it's quite simple, you're clicking right mouse button on the connection and choosing "Connect"/"Disconnect" command from the context menu. Is there any programmatic analogue of that action? It seemed rather simple to me but eventually I've found nothing on this matter. I found how to check a connection status, how to enumerate all the network interfaces, how to dial a 3G modem connection, how to enable/disable a network card, how to monitor availability of the net, etc, anything but this! Generally I need a software switcher that will be able to turn on and off an ethernet connection (not a network card!).

Isn't there a simple method something like GetConnection(connectionName).Connect()?

2

There are 2 best solutions below

1
On

Make some searches on TCP/IP Client/Server modeling in c#, you actually can start by some already existing sample codes like this

2
On

Ethernet doesn't have a concept of "connected" or "disconnected". It is really a multiple random access medium. Also, I'm not sure what UI element you are talking about. On my machine, I can "connect/disconnect" wireless adapters, but not Ethernet adapters. This is because most wireless protocols have an actual concept of being "connected" to some access point. If you are really talking about a wireless adapter, I'm sure there are windows API's that can control them, but I don't know off hand where to point you. Otherwise, I think you might need to take a step back and explain the larger context of what you want to accomplish.