C# Win32_NetworkAdapterConfiguration.NET 4-设置静态IP在XP上有效,但在Windows 7上无效
我有一些代码,我目前正在使用它来更改网络适配器的静态IP。在Windows XP 32位计算机(PHSYIC和VM)上运行时,设置IP地址时会有一个轻微的暂停(~1秒),但它似乎会更改IP 在Windows 7 64位计算机上运行时,它无法更改IP地址。当它尝试进行更改时,不会暂停,也不会引发异常 我在谷歌上做了大量的搜索,大多数建议似乎只是简单地以管理员的身份运行。我尝试右键单击可执行文件并选择“以管理员身份运行”,我尝试创建快捷方式并将其设置为以管理员身份运行,我尝试更新清单文件(它在启动时要求管理员权限,但仍然不更改IP地址) 有人能提供一些建议吗 代码如下:C# Win32_NetworkAdapterConfiguration.NET 4-设置静态IP在XP上有效,但在Windows 7上无效,c#,windows-7,static-ip-address,C#,Windows 7,Static Ip Address,我有一些代码,我目前正在使用它来更改网络适配器的静态IP。在Windows XP 32位计算机(PHSYIC和VM)上运行时,设置IP地址时会有一个轻微的暂停(~1秒),但它似乎会更改IP 在Windows 7 64位计算机上运行时,它无法更改IP地址。当它尝试进行更改时,不会暂停,也不会引发异常 我在谷歌上做了大量的搜索,大多数建议似乎只是简单地以管理员的身份运行。我尝试右键单击可执行文件并选择“以管理员身份运行”,我尝试创建快捷方式并将其设置为以管理员身份运行,我尝试更新清单文件(它在启动时
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management; // You will need to add a reference for System.Management!
namespace NetAdapt
{
class Program
{
static void Main(string[] args)
{
// First display all network adaptors
DisplayNetworkAdaptors(-1);
// Now try to set static IP, subnet mask and default gateway for adaptor with
// index of 1 (may be different for your machine!)
SetIP(1,
new string[] { "10.10.1.222" },
new string[] { "255.255.255.0" },
new string[] { "10.10.1.10" });
// Now display network adaptor settings for adaptor 1 (may be different for your machine!)
DisplayNetworkAdaptors(1);
Console.ReadLine();
}
private static void SetIP(int index, string[] newIPAddress, string[] newSubnetMask, string[] newGateway)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["IPEnabled"]) continue;
try
{
//Only change for device specified
if ((uint)objMO["Index"] == index)
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");
objNewGate["DefaultIPGateway"] = newGateway;
objNewIP["IPAddress"] = newIPAddress;
objNewIP["SubnetMask"] = newSubnetMask;
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);
Console.WriteLine("Successfully changed IP!");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception setting IP: " + ex.Message);
}
}
}
private static void DisplayNetworkAdaptors(int index)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
try
{
// TCP enabled NICs only
if (!(bool)objMO["IPEnabled"]) continue;
// If index is -1 then display all network adaptors, otherwise only
// display adaptor whose index matches parameter
if ((index != -1) && ((uint)objMO["Index"] != index)) continue;
Console.WriteLine("Caption : " + (string)objMO["Caption"]);
string[] defaultGateways=(string[])objMO["DefaultIPGateway"];
if (defaultGateways != null)
{
for (int x = 0; x < defaultGateways.Count(); x++)
{
Console.WriteLine(string.Format("DefaultIPGateway{0} : {1}", x, defaultGateways[x]));
}
}
else
{
Console.WriteLine("DefaultIPGateway : NULL");
}
Console.WriteLine("Description : " + (string)objMO["Description"]);
Console.WriteLine("DHCPEnabled : " + (bool)objMO["DHCPEnabled"]);
Console.WriteLine("DHCPServer : " + (string)objMO["DHCPServer"]);
Console.WriteLine("Index : " + (uint)objMO["Index"]);
string[] ipAddresses = (string[])objMO["IPAddress"];
if (ipAddresses != null)
{
for (int x = 0; x < ipAddresses.Count(); x++)
{
Console.WriteLine(string.Format("IPAddress{0} : {1}", x, ipAddresses[x]));
}
}
else
{
Console.WriteLine("IPAddress : NULL");
}
Console.WriteLine("IPEnabled : " + (bool)objMO["IPEnabled"]);
string[] ipSubnets = (string[])objMO["IPSubnet"];
if (ipSubnets != null)
{
for (int x = 0; x < ipSubnets.Count(); x++)
{
Console.WriteLine(string.Format("IPSubnet{0} : {1}", x, ipSubnets[x]));
}
}
else
{
Console.WriteLine("IPSubnet : NULL");
}
Console.WriteLine("MACAddress : " + (string)objMO["MACAddress"]);
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception getting network adaptors: " + ex.Message);
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Management;//您需要为System.Management添加一个参考!
名称空间NetAdapt
{
班级计划
{
静态void Main(字符串[]参数)
{
//首先显示所有网络适配器
显示网络适配器(-1);
//现在尝试为适配器设置静态IP、子网掩码和默认网关
//索引为1(可能与您的机器不同!)
SetIP(1,
新字符串[]{“10.10.1.222”},
新字符串[]{“255.255.255.0”},
新字符串[]{“10.10.1.10”});
//现在显示适配器1的网络适配器设置(对于您的机器可能不同!)
显示网络适配器(1);
Console.ReadLine();
}
私有静态void SetIP(int索引,字符串[]newIPAddress,字符串[]newSubnetMask,字符串[]newGateway)
{
ManagementClass objMC=新的ManagementClass(“Win32_网络适配器配置”);
ManagementObjectCollection objMOC=objMC.GetInstances();
foreach(objMOC中的managementobjectobjmo)
{
如果(!(bool)objMO[“IPEnabled”])继续;
尝试
{
//仅对指定的设备进行更改
如果((uint)objMO[“索引”]==索引)
{
ManagementBaseObject objNewIP=null;
ManagementBaseObject objSetIP=null;
ManagementBaseObject objNewGate=null;
objNewIP=objMO.GetMethodParameters(“EnableStatic”);
objNewGate=objMO.GetMethodParameters(“SetGateways”);
objNewGate[“DefaultIPGateway”]=newGateway;
objNewIP[“IPAddress”]=新IPAddress;
objNewIP[“子网掩码”]=新闻子网掩码;
objSetIP=objMO.InvokeMethod(“EnableStatic”,objNewIP,null);
objSetIP=objMO.InvokeMethod(“SetGateways”,objNewGate,null);
Console.WriteLine(“已成功更改IP!”);
}
}
捕获(例外情况除外)
{
Console.WriteLine(“异常设置IP:+ex.Message”);
}
}
}
专用静态void显示网络适配器(int索引)
{
ManagementClass objMC=新的ManagementClass(“Win32_网络适配器配置”);
ManagementObjectCollection objMOC=objMC.GetInstances();
foreach(objMOC中的managementobjectobjmo)
{
尝试
{
//仅支持TCP的NIC
如果(!(bool)objMO[“IPEnabled”])继续;
//如果索引为-1,则显示所有网络适配器,否则仅显示
//索引与参数匹配的显示适配器
如果((索引!=-1)和((uint)objMO[“索引”!=index))继续;
WriteLine(“标题:”+(字符串)objMO[“标题”]);
字符串[]defaultGateways=(字符串[])objMO[“DefaultIPGateway”];
if(defaultGateways!=null)
{
对于(int x=0;x
好的,通过检查代码项目中的以下内容,最终找到了问题的解决方案:
在上述作者的代码中,他使用以下代码设置静态IP:
objMO.InvokeMethod("EnableStatic", new object[] { newIPAddress, newSubnetMask });
objMO.InvokeMethod("SetGateways", new object[] { newGateway, new string[] { "1" } });
…在Windows 7上运行良好,但在Windows XP上不起作用。在我自己的代码中,我求助于询问System.Environment.OSVersion.Version并选择我的方法o