Dynamically change IP Address

  • Thread starter Thread starter DMA
  • Start date Start date
D

DMA

How can I change my IP address ?
I try to do it in C#.
It's easy to get the IP but to fix it is another problem..

Thanks.
 
I have found something about that and actually I am trying to get it
work.
Thank you.
I will see on monday.
I just had to add a reference to System.Management that is not by
default in C# Express.
 
That code works very well ! :
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new
ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
try
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;

inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = new string[] {
"10.59.245.186" };
inPar["SubnetMask"] = new string[] {
"255.255.0.0" };
outPar = mo.InvokeMethod("EnableStatic", inPar,
null);
MonIP.Text = "10.59.245.186";
}
}
catch (Exception ex)
{
MessageBox.Show("Problème au changement d'IP (" +
ex.Message + ")", "erreur", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
 
Back
Top