Dobrý den, mohli byste mi prosím poradit, jak nastavit automatickou IP z DHCP serveru? (viz. komentář ve funkci níže) D9ky moc
public static bool ApplyAdapter(string AdapterName, ItemClass ItempIP)
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
bool ret = false;
foreach (ManagementObject mo in moc)
{
if (mo["Caption"].Equals(AdapterName))
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objNewGate = null;
objNewIP = mo.GetMethodParameters("EnableStatic");
objNewGate = mo.GetMethodParameters("SetGateways");
//Set DefaultGateway
objNewGate["DefaultIPGateway"] = new string[] { ItempIP.Gate };
objNewGate["GatewayCostMetric"] = new int[] { 1 };
//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] { ItempIP.IP };
objNewIP["SubnetMask"] = new string[] { ItempIP.Mask };
if (!ItempIP.IPautomatic)
{
// pevně daná IP
mo.InvokeMethod("EnableStatic", objNewIP, null);
mo.InvokeMethod("SetGateways", objNewGate, null);
}
else
{
//Automatická IP ze serveru DHCP
// zde právě nevím, jak toho docílit ... prosím o radu
}
// DNS
ManagementBaseObject mboDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
if (ItempIP.DNSautomatic)
mboDNS["DNSServerSearchOrder"] = null;
else
{
List<string> list = new List<string>();
if (ItempIP.DefaultPrinter != string.Empty)
list.Add(ItempIP.DNS1);
if (ItempIP.DNS2 != string.Empty)
list.Add(ItempIP.DNS2);
mboDNS["DNSServerSearchOrder"] = list.ToArray<string>();
}
mo.InvokeMethod("SetDNSServerSearchOrder", mboDNS, null);
ret = true;
break;
}
}
if (moc != null)
moc.Dispose();
if (mc != null)
mc.Dispose();
return ret;
}
public class ItemClass
{
public string Name;
public bool IPautomatic;
public string IP;
public string Mask;
public string Gate;
public bool DNSautomatic;
public string DNS1;
public string DNS2;
}
|