■ IP 주소 변경하기 예제

----------------------------------------------------------------------------------------------------

bool result = ChangeIPAddress("802.11n Wireless LAN Card", "192.168.0.10", "255.255.255.0", "192.168.0.1");

----------------------------------------------------------------------------------------------------

※ sourceDescription은 명령 프롬프트에서 ipconfig/all을 실행해 출력되는 '설명' 값이다.

※ DHCP를 설정하는 경우 sourceDescription만 설정하고 나머지 값들은 string.Empty로 설정하고 호출한다.

 

■ IP 주소 변경하기

----------------------------------------------------------------------------------------------------

using System;

using System.Management;

 

#region IP 주소 변경하기 - ChangeIPAddress(sourceDescription, sourceIPAddress, sourceSubnetMask, sourceGateway)

 

/// <summary>

/// IP 주소 변경하기

/// </summary>

/// <param name="sourceDescription">소스 설명</param>

/// <param name="sourceIPAddress">소스 IP 주소</param>

/// <param name="sourceSubnetMask">소스 서브넷 마스크</param>

/// <param name="sourceGateway">소스 게이트웨이</param>

/// <returns>처리 결과</returns>

public bool ChangeIPAddress(string sourceDescription, string sourceIPAddress, string sourceSubnetMask,

    string sourceGateway)

{

    ManagementClass            managementClass            = new ManagementClass("Win32_NetworkAdapterConfiguration");

    ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();

 

    foreach(ManagementObject managementObject in managementObjectCollection)

    {

        string description = managementObject["Description"] as string;

 

        if(string.Compare(description, sourceDescription, StringComparison.InvariantCultureIgnoreCase) == 0)

        {

            try

            {

                ManagementBaseObject setGatewaysManagementBaseObject =

                    managementObject.GetMethodParameters("SetGateways");

 

                setGatewaysManagementBaseObject["DefaultIPGateway" ] = new string[] { sourceGateway };

                setGatewaysManagementBaseObject["GatewayCostMetric"] = new int[] { 1 };

 

                ManagementBaseObject enableStaticManagementBaseObject =

                    managementObject.GetMethodParameters("EnableStatic");

 

                enableStaticManagementBaseObject["IPAddress" ] = new string[] { sourceIPAddress  };

                enableStaticManagementBaseObject["SubnetMask"] = new string[] { sourceSubnetMask };

 

                managementObject.InvokeMethod("EnableStatic", enableStaticManagementBaseObject, null);

                managementObject.InvokeMethod("SetGateways" , setGatewaysManagementBaseObject , null);

 

                return true;

            }

            catch

            {

                return false;

            }

        }

    }

 

    return true;

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요