????????Ч??????£?

???????????

??????1??????
??????????? Windows Management Instrumentation??????????????????????????????? Management???á?
?????????NetworkAdapter??????????????IP??????????????????????IP????? ????NetworkAdapterUtil(?????NetworkAdapter?????? )??????
??????windows8 ?????app.manifest?????
???????????  <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />????????й??????????????IP?????
????1 ????IP??????????????IP????????IP???????????????
/// <summary>
/// ????????????
/// </summary>
public class NetworkAdapter
{
/// <summary>
/// ????IP???
/// </summary>
/// <param name="ip"></param>
/// <param name="submask"></param>
/// <param name="getway"></param>
/// <param name="dns"></param>
private bool SetIPAddress(string[] ip?? string[] submask?? string[] getway?? string[] dns)
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
string str = "";
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
if (this.NetworkInterfaceID == mo["SettingID"].ToString())
{
if (ip != null && submask != null)
{
string caption = mo["Caption"].ToString(); //????
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = ip;
inPar["SubnetMask"] = submask;
outPar = mo.InvokeMethod("EnableStatic"?? inPar?? null);
str = outPar["returnvalue"].ToString();
return (str=="0"||str=="1")?true:false;
//???????????IP???????? ?????????????IP??????ó???? 0??1??????
// ????????????? https://msdn.microsoft.com/en-us/library/aa393301(v=vs.85).aspx
}
if(getway!=null)
{
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = getway;
outPar = mo.InvokeMethod("SetGateways"?? inPar?? null);
str = outPar["returnvalue"].ToString();
return (str == "0" || str == "1") ? true : false;
}
if (dns != null)
{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = dns;
outPar = mo.InvokeMethod("SetDNSServerSearchOrder"?? inPar?? null);
str = outPar["returnvalue"].ToString();
return (str == "0" || str == "1") ? true : false;
}
}
}
return false;
}
??