Easy way to create and remove loopback network interfaces on Windows (.NET Framework)
Utilizes devcon and netsh.
An important note about this library - To use devcon.exe it must be run as an elevated process. This is currently enforced by making a check in LoopbackNET that the current process is elevated when trying to create new loopbacks.
Create new unnamed/name interface:
LoopbackNET.Loopback.Create();
LoopbackNET.Loopback.Create("myLoopback");
If you want to remove the interface when you are done, keep the result:
LoopbackInterface iface = LoopbackNET.Loopback.Create();
... more code ...
iface.Remove();
Another option is to use the 'using' keyword since LoopbackInterface
implements IDisposable
:
using(LoopbackInterface iface = LoopbackNET.Loopback.Create())
{
... more code ...
}
While SharpPcap is not a requirement for this library, you might want to use your newly created loopback interface with a SharpPcap device object.
You can use the NetworkInterface ID of the created loopback to find the right device like so:
LoopbackInterface iface = LoopbackNET.Loopback.Create();
string interfaceId = iface.NetworkInterface.Id;
ICaptureDevice dev = SharpPcap.CaptureDeviceList.Instance.Single(device => device.Name.Contains(interfaceId));