开发者

How to clear the default gateway of a network interface (nic) using C#

开发者 https://www.devze.com 2023-03-23 07:31 出处:网络
I\'m currently changing the default gateway using: ManagementBaseObject gateway = mo.GetMethodParameters(\"SetGateways\");

I'm currently changing the default gateway using:

ManagementBaseObject gateway = mo.GetMethodParameters("SetGateways");
gateway["DefaultIPGateway"] = new string[] { "192.168.0.1" };
var r = mo.InvokeMethod("SetGateways", gateway, null);

What I want to know is how can I clear the gateway, I tried empty string and null values as parameters instead of "192.168.0.1" but didn't work. Anyone has any ideas about this?

Than开发者_C百科ks


Set your gateway to the same IP as the static IP for the NIC. Not sure why but this clears the gateway.


There are a number of configurations that will result in the invocation failing, such as DHCP being enabled, that are not related to WMI. The code itself is probably fine, assuming mo is a Win32_NetworkAdapterConfiguration instance.

If you haven't already, you may want to use the value of r.Properties["returnvalue"].Value to get the return value, and check that the return value is what you expect (MSDN ref). The error code should narrow down the issue.


Set the gateway to an empty array.

ManagementBaseObject gateway = mo.GetMethodParameters("SetGateways");
gateway["DefaultIPGateway"] = new string[] {};
var r = mo.InvokeMethod("SetGateways", gateway, null);


This method only works when the Network Interface Card (NIC) is in the static IP mode. To clear the gateway, set your gateway to the same IP you use on EnableStatic.

Please see "Remark" of the follow page: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/setgateways-method-in-class-win32-networkadapterconfiguration

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号