C # dynamically allocate IP addresses and release IP addresses

Source: Internet
Author: User
C # dynamically allocate IP addresses and release IP addresses. Recently, you need to dynamically allocate and release IP addresses. It is easy to implement in C ++. However, because of the Network IPThe configuration involves hardware, and C # does not have a ready-made interface to call. You can only call an API or WMIThis system provides us with a bridge, mainly through" Win32_NetworkAdapterConfiguration"This management class.

The Demo I wrote is attached below. I hope it will help my friends who need it, and I also hope that experts can make bricks.

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Management;
Using System. Management. Instrumentation;
Using System. Collections;
Namespace J_Queen
{
Class Program
{
Static void Main (string [] args)
{
Dictionary <string, ManagementObject> allDevices = new Dictionary <string, ManagementObject> (); // Save the management object
List <string> listDescription = new List <string> (); // Save the NIC description

// Obtain management instance and management object
ManagementClass classInstance = new ManagementClass ("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection bjectCollection = classInstance. GetInstances ();

Foreach (ManagementObject obj in objectCollection)
{
// Skip this step if no network device with IP settings Enabled
If (! (Bool) obj ["IPEnabled"])
{
Continue;
}

// Store related information
AllDevices. Add (string) obj ["Description"], obj );
ListDescription. Add (string) obj ["Description"]);
}
For (int I = 0; I <listDescription. Count; I ++)
{
Console. WriteLine (I. ToString () + ":" + listDescription [I]);
}
Console. Write ("Enter the number (q: Quit) and select NIC :");
String xInput = Console. ReadLine ();
Int deviceNumber;
While (xInput! = "Q ")
{
If (! Int32.TryParse (xInput, out deviceNumber ))
{
Console. Write ("input error, re-input :");
XInput = Console. ReadLine ();
Continue;
}
If (deviceNumber> listDescription. Count-1 | deviceNumber <0)
{
Console. Write ("the number entered exceeds the range. re-enter :");
XInput = Console. ReadLine ();
Continue;
}
If (allDevices. ContainsKey (listDescription [deviceNumber])
{
Console. WriteLine ("1: Release IP (Release), 2: Re-obtain IP (Renew )");
String ptionInput = Console. ReadLine ();
Int option;
If (Int32.TryParse (optionInput, out option ))
{
If (option = 1)
{
Int returnValue = IpRelease (ManagementObject) allDevices [listDescription [deviceNumber]);
If (returnValue <2)
{
Console. WriteLine ("successfully released IP Address ");
Console. Write ("Enter the number of the NIC above (q: Quit), select NIC :");
XInput = Console. ReadLine ();
Continue;
}
}
Else if (option = 2)
{
Int returnValue = IpRenew (ManagementObject) allDevices [listDescription [option]);
If (returnValue <2)
{
Console. WriteLine ("IP allocated successfully ");
Console. Write ("Enter the number of the NIC above (q: Quit), select NIC :");
XInput = Console. ReadLine ();
Continue;
}
}
Else
{
Console. WriteLine ("incorrect selection ");
Console. Write ("Enter the number of the NIC above (q: Quit), select NIC :");
Console. ReadLine ();
Continue;
}
}
}
}
}
/// <Summary>
/// Function: reassign the IP address of the specified Nic
/// </Summary>
/// <Param name = "obj"> ManagementObject obj -- the management object of the corresponding Nic </param>
/// <Returns> return value, integer, 0, and 1 indicate success </returns>
Public static int IpRenew (ManagementObject obj)
{
ManagementBaseObject utPar = null;
UtPar = obj. InvokeMethod ("RenewDHCPLease", null, null );
Return Convert. ToInt32 (outPar ["returnValue"]);
}
/// <Summary>
/// Function: releases the IP address of the specified Nic.
/// </Summary>
/// <Param name = "obj"> ManagementObject obj -- the management object of the corresponding Nic </param>
/// <Returns> return value, integer, 0, and 1 indicate success </returns>
Public static int IpRelease (ManagementObject obj)
{
ManagementBaseObject utPar = null;
UtPar = obj. InvokeMethod ("ReleaseDHCPLease", null, null );
Return Convert. ToInt32 (outPar ["returnValue"]);
}
}
}

Posted @ J_Queen Lau read (751) Comments (2) EDIT favorites

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.