C # enable Nic IP address Switching

Source: Internet
Author: User

Requirement: the laptop often needs to connect to the network in different locations, and the network IP addresses in different regions are different, which leads to changing the IP address settings of the NIC from time to time. It is inconvenient to change the IP settings on windows, so we hope to develop a Fast IP address switch that applies to different networks and network adapters.Program.

Analysis: The key lies in how to configure the network adapter. In fact, Ms provides us with the managementclass class in the system. Management namespace, through which we can obtain and set information about all the adapters on the computer. Second, you need to store configuration information. This is simple. You can serialize the NIC information and save it to a file, and then deserialize it.

Implementation: after solving the above two problems, everything is simple. Let's get started with the final effect ()

Here we will focus on the managementclass class: it represents the Common Information Model (CIM) management class. The management class is a WMI class, such as win32_logicaldisk and win32_process. The former indicates the disk drive, and the latter indicates the process (such as notepad.exe). Then the network adapter is win32_networkadapterconfiguration. For more information about the win32_networkadapterconfiguration class (such as its attributes and methods), refer to the relevant description on msdn.

Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();

In this way, you can easily obtain all the network card instances on your computer. We can use managementobject to represent a WMI instance. We can configure the corresponding Nic IP Address by calling the invokemethod method of the instance. For example:

Networkadapter. invokemethod ("enabledhcp", null, null); // by calling the enabledhcp method of the win32_networkadapterconfiguration class, set the networkadapter of the NIC instance to automatically obtain the IP address (that is, enable the DHCP function)

Managementbaseobject paripsetting = networkadapter. getmethodparameters ("enablestatic"); // for methods of the win32_networkadapterconfiguration class with parameters, you must first use the getmethodparameters method to obtain the parameter object and assign a value to the parameter.
Paripsetting ["IPaddress"] = new string [] {IPaddress };
Paripsetting ["subnetmask"] = new string [] {subnetmask };
Networkadapter. invokemethod ("enablestatic", paripsetting, null); // This is an example of setting the IP address and subnet mask.

With the above introduction, I believe that as long as you are careful, you can use msdn to describe win32_networkadapterconfiguration fields and methods, you can quickly complete the configuration of the default gateway and DNS server for the remaining related NICs.

conclusion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.