1

I have a C# application in which I need to detect a USB to Ethernet adapter connected to a PC and set it with a static IP address. The adapter can be of any brand, I don't know which one the end user will have. All I do know is that it will be only one USB to Ethernet adapter connected to that PC.

Here is my current code which should have worked:

public static bool SetStaticIPAddress(out string exception)
{
    const string USBtoEthernetAdapter = "USB to Ethernet Adapter";

    var adapterConfig = new ManagementClass("Win32_NetworkAdapterConfiguration");
    var networkCollection = adapterConfig.GetInstances();
    foreach (ManagementObject adapter in networkCollection)
    {
        string description = adapter["Description"] as string;

        if (description.StartsWith(USBtoEthernetAdapter, StringComparison.InvariantCultureIgnoreCase))
        {
            try
            {
                var newAddress = adapter.GetMethodParameters("EnableStatic");
                newAddress["IPAddress"] = new string[] { "10.0.0.2"};
                newAddress["SubnetMask"] = new string[] { "255.255.255.0"};

                adapter.InvokeMethod("EnableStatic", newAddress, null);
                exception = null;
                return true;
            }
            catch (Exception ex)
            {
                exception = $"Unable to Set IP: {ex.Message}";
                return false;
            }
        }
    }
    exception = $"Could not find any \"{USBtoEthernetAdapter}\" device connected";
    return false;
}

But, unfortunately , it doesn't and when I run ipconfig the IP is not set to 10.0.0.2 and this makes other parts of my app that tries to ping it to fail...

When I debug the code I see that it does find a ManagementObject with the description "USB to Ethernet Adapter" but its IP doesn't change.

I searched the web and found on MSDN this code sample that uses NetworkInterface so I tried this:

NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
    IPInterfaceProperties properties = adapter.GetIPProperties();
    // This is the name of the USB adapter but I can't relay on this name
    // since I don't know which device the user will use except for the fact
    // that is will be a USB to Ethernet Adapter...
    if (adapter.Description == "Realtek USB FE Family Controller")
    {
        // No way to set the device IP here...
    }
}

Is there a 100% way to detect that single USB adapter and set it with the static IP?

Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • 1
    Out of curiosity. Why do you want to do that. – L.B Sep 13 '17 at 12:19
  • 1
    Are you running this code with administrative permissions? Changing parameters of network interfaces requires that. – Alejandro Sep 13 '17 at 12:22
  • I need to develop a hardware test software for a factory and one of the tests is an Ethernet check, so the factory will connect a network cable from the hardware the produce to a PC via a USB to Ethernet Adapter and I need to set a static IP to the hardware and to the PC and then do a `ping` to the hardware. – Liran Friedman Sep 13 '17 at 12:22
  • @Alejandro - Yes, I am. The installation of my software requires Admin permission, but I don't have a special check for admin in this related code. – Liran Friedman Sep 13 '17 at 12:25
  • @Alejandro - Yes, I am. The installation of my software requires Admin permission, but I don't have a special check for admin in this related code. – Liran Friedman Sep 13 '17 at 12:25
  • 1
    @LiranFriedman But is this snippet part of the installation? I'm pretty sure this particular code fragment needs administrator privileges, not only during install, as it changes a global system configuration. Could you add that check for admin permissions to this particular part? – Alejandro Sep 13 '17 at 12:56
  • @Alejandro - Can you please provide a code snippet to perform this check ? and maybe a way for the software to request the admin permission to solve this issue ? – Liran Friedman Sep 13 '17 at 13:11
  • @Alejandro - I have `` in my `app.manifest` file. Do you know on any other way to get admin privileges ? – Liran Friedman Sep 13 '17 at 13:24
  • As a quick test, I would run Visual Studio as administrator, debug there and see if it succeeds. Add the check if it does. As for the check code itself, look here: [Check if the current user is administrator](https://stackoverflow.com/a/3600338/2557263). You can error-out if the check fails, asking the user to run the program as administrator. A better way could be to try to launch an elevated process or manifest the whole program to require administrator on startup: [Elevating process privilege programmatically?](https://stackoverflow.com/q/133379/2557263). – Alejandro Sep 13 '17 at 13:25
  • @Alejandro - I know for sure that I'm running Visual Studio as Admin, and when launching, the app also prompts the user for admin privileges. So I don't know which other way for admin there is... – Liran Friedman Sep 13 '17 at 13:29

0 Answers0