Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6145903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:51:54+00:00 2026-05-23T18:51:54+00:00

I’m trying to create a tool that converts the dynamic DHCP-provided IPv4 address, gateway

  • 0

I’m trying to create a tool that converts the dynamic DHCP-provided IPv4 address, gateway and dns-settings into static configuration. I’ve tried to use WMI to solve this puzzle, but I have a problem I can’t figure out.

The application completes, DNS and Gateway is configured, but the EnableStatic method (to set the IP address and subnet) has been unsuccesful which means that the IP is still received from DHCP (with greyed out fields) even though the default gateway has been set. How do I fix this?

The ReturnValue from EnableStatic is 70 (Invalid IP address). The weird thing is that the input parameters are the same that I extracted from the NIC 2 seconds earlier.

Here is the code (except GUI), http://pastebin.com/AE3dGhUz:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace Static_NIC_Settings_Creator
{
    public partial class Form1 : Form
    {
        private ManagementObjectCollection queryCollection;
        private string[] networkInterfaces;
        private int currentNIC;
        private string[] ipAddress;
        private string[] subnetMask;
        private string[] defaultIPGateway;
        private string[] dnsServerSearchOrder;

        public Form1()
        {
            InitializeComponent();
            getNICs();
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            if (networkInterfaces.Count() > 0)
            {
                //Get current NIC settings
                if (!getNICSettings())
                {
                    MessageBox.Show("Retrieving current NIC settings failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //Convert to static NIC settings
                if (!setNICStatic())
                {
                    MessageBox.Show("Setting NIC settings to static failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void nicSelecter_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentNIC = nicSelecter.SelectedIndex;
        }

        private void getNICs()
        {
            //Get NICS
            ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
            queryCollection = query.Get();
            //Make nic string array
            int i = queryCollection.Count;
            networkInterfaces = new string[i];
            //Fill nic string array
            i = 0;
            foreach (ManagementObject mo in queryCollection)
            {
                networkInterfaces[i] = (String)mo["Description"];
                i++;
            }
            //Fill dropbox with arraylist-data
            nicSelecter.DataSource = networkInterfaces;
        }

        private Boolean getNICSettings()
        {
            //Get selected NIC
            int i = 0;
            foreach (ManagementObject mo in queryCollection)
            {
                //Get settings for specific NIC
                if (i == currentNIC)
                {
                    try
                    {
                        ipAddress = (String[])mo["IPAddress"];
                        subnetMask = (String[])mo["IPSubnet"];
                        defaultIPGateway = (String[])mo["DefaultIPGateway"];
                        dnsServerSearchOrder = (String[])mo["DNSServerSearchOrder"];
                        return true;
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
                        return false;
                    }
                }
                i++;
            }
            return false;
        }

        private Boolean setNICStatic()
        {
            //Get selected NIC
            int i = 0;
            foreach (ManagementObject mo in queryCollection)
            {
                //Get settings for specific NIC
                if (i == currentNIC)
                {
                    try
                    {
                        //Set static IP and subnet mask
                        ManagementBaseObject setIP;
                        ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
                        newIP["IPAddress"] = ipAddress;
                        newIP["SubnetMask"] = subnetMask;
                        setIP = mo.InvokeMethod("EnableStatic", newIP, null);
                        //Set default gateway
                        ManagementBaseObject setGateway;
                        ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
                        newGateway["DefaultIPGateway"] = defaultIPGateway;
                        newGateway["GatewayCostMetric"] = new int[] { 1 };
                        setGateway = mo.InvokeMethod("SetGateways", newGateway, null);
                        //Set dns servers
                        ManagementBaseObject setDNS;
                        ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
                        newDNS["DNSServerSearchOrder"] = dnsServerSearchOrder;
                        setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                        System.Windows.Forms.MessageBox.Show("Setting NIC settings returned: " + setDNS);
                        return true;
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error");
                        return false;
                    }
                }
                i++;
            }
            //No NICs
            return false;
        }
    } //End class
}

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:51:55+00:00Added an answer on May 23, 2026 at 6:51 pm

    Could it be that you are inputting IPv6 addresses as well? Just playing around with PowerShell it seems not to like them. Perhaps you can post actual values that are being inputted whilst debugging, it would help a lot. Also maybe try statically inputting some values like:

    new string[]{"192.168.0.1"}, new string[] {"255.255.255.255"}
    

    Also unless you really, really need C# and a GUI you may want to consider using PowerShell (requirement is it being installed of course) as WMI is really much simpler to manipulate there (sadly you still have that learning curve though).

    This is just an example of how to use PowerShell, you can at the very least use it for some testing:

    Get-WmiObject Win32_NetworkAdapterConfiguration
    

    Then get the index of your adapter then run, but replace your index number:

    $obj = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.Index -eq 1}
    $obj.EnableStatic("192.168.0.1", "255.255.255.0")
    

    To get method parameters just run:

    $obj.EnableStatic
    

    It will return:

    MemberType          : Method
    OverloadDefinitions : {System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)}
    TypeNameOfValue     : System.Management.Automation.PSMethod
    Value               : System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)
    Name                : EnableStatic
    IsInstance          : True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.