Answer the question
In order to leave comments, you need to log in
How to change the ip address of a network card using C#?
Good afternoon!
I am writing a program that, under certain conditions, will have to change ip, default gateway and DNS without restarting the computer.
I found a solution to change these settings through the registry, but you need to restart the PC.
Tell me how to change the settings without turning off the PC ...
Answer the question
In order to leave comments, you need to log in
I changed it like this (in public properties I set the required parameters, then called set):
(scroll left-right shift-wheel)
using System.Management;
private class StaticIP : IConfigurationIP
{
// mac-адрес адаптера, которому присваиваются настройки:
public String macAddress;
public string[] IPAddress;
public string[] IPSubnet;
public string[] DefaultIPGateway;
public string[] DNSServerSearchOrder;
public UInt16[] GatewayCostMetric;
public int proxyStatus;
/// <summary>
/// Установить конфигурацию сетевого адаптера, используя имеющиеся в классе настройки.
/// В принципе было бы корректно освобождать IP-адрес перед установкой статического адреса. см. msdn.microsoft.com/en-us/library/aa393047(v=vs.85).aspx
/// (ReleaseDHCPLease)
/// </summary>
void IConfigurationIP.set()
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
// Параметры objMO: http://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx
// Найти сетевую карту с mac-адресом, для которой предназначена эта настройка:
string _macAddress = (string)objMO["MACAddress"];
if (_macAddress.Equals(this.macAddress))
{
// http://www.java2s.com/Tutorial/CSharp/0580__Network/GetIPaddressfromWin32NetworkAdapterConfiguration.htm
// http://msdn.microsoft.com/en-us/library/aa394217%28v=vs.85%29.aspx
ManagementBaseObject EnableStatic = objMO.GetMethodParameters("EnableStatic");
ManagementBaseObject EnableStatic_ret;
EnableStatic["IPAddress"] = this.IPAddress;
EnableStatic["SubnetMask"] = this.IPSubnet;
EnableStatic_ret = objMO.InvokeMethod("EnableStatic", EnableStatic, null);
ManagementBaseObject DNSServerSearchOrder = objMO.GetMethodParameters("SetDNSServerSearchOrder");
ManagementBaseObject DNSServerSearchOrder_ret;
DNSServerSearchOrder["DNSServerSearchOrder"] = this.DNSServerSearchOrder;
DNSServerSearchOrder_ret = objMO.InvokeMethod("SetDNSServerSearchOrder", DNSServerSearchOrder, null);
ManagementBaseObject SetGateways = objMO.GetMethodParameters("SetGateways");
ManagementBaseObject SetGateways_ret;
SetGateways["DefaultIPGateway"] = this.DefaultIPGateway;
SetGateways["GatewayCostMetric"] = this.GatewayCostMetric;
SetGateways_ret = objMO.InvokeMethod("SetGateways", SetGateways, null);
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", this.proxyStatus);
//proxyStatus = (int)registry.GetValue("ProxyEnable");
Console.WriteLine(" set {0} to {1} finished", this.macAddress, this.IPAddress[0]);
return;
}
}
catch (Exception)
{
throw;
}
}
}
}
}
It doesn't matter from which PL you can call the operating system command:
ipconfig / release
ipconfig / renew
this is provided that there is one network interface, if there are several, then you need to specify which one ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question