是否有人知道是否存在包含WMI IPHelper功能等效的现有C#.NET命名空间?具体来说,我需要调用CreateIpForwardEntry和DeleteIpForwardEntry。通过P / Invoke是唯一的方法吗? (注意:这不是对第三方库的请求,而是对标准.NET库的请求)
答案 0 :(得分:3)
最后我决定不去尝试P / Invoke WMI IPHelper功能。只需启动进程打开cmd.exe并编写控制台命令来添加和删除路由就容易得多。下面的代码说明了如何做到这一点。
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.StandardInput.WriteLine(delete
? "route delete " + ServiceIpAddress + " mask " + GatewayMask + " " + GatewayIpAddress + " -p" : "route add " + ServiceIpAddress + " mask " + GatewayMask + " " + GatewayIpAddress + " -p");
roc.Close();