我有一些代碼,目前我正在使用它來更改網絡適配器的靜態IP。在Windows XP 32位計算機(phsyical和VM)上運行時,在設置IP地址時會稍微暫停(約1秒),但似乎確實會改變IP。Win32_NetworkAdapterConfiguration .NET 4 - 設置靜態IP在XP上工作但在Windows 7上不能工作
在Windows 7 64位機器上運行時,無法更改IP地址。嘗試進行更改時沒有暫停,也不會引發異常。
我已經做了相當多的谷歌搜索,大多數建議似乎只是以管理員身份運行。我嘗試右鍵單擊可執行文件並選擇「以管理員身份運行」,我嘗試創建一個快捷方式並將其設置爲以管理員身份運行,並且我嘗試更新Manifest文件(它要求管理員權限啓動,但仍不會更改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);
}
}
}
}
}
您確定您的目標設備是正確的? Windows 7有更多的網絡適配器比你想象的要多... – KingCronus
是的,我確定我正在尋址正確的網絡適配器。 DisplayNetworkAdaptors()顯示的標題與ControlPanel | NetworkConnections中的標題相匹配(我可以從中手動更改靜態IP,而不會出現問題。) – JamesPD