Reputation: 4310
Does anyone know if there is an existing C#.NET namespace that contains the equivalent of the WMI IPHelper functions? Specifically I need to call CreateIpForwardEntry and DeleteIpForwardEntry. Is the only way via P/Invoke? (NOTE: this isn't a request for third-party libraries, but for standard .NET libraries)
Upvotes: 1
Views: 199
Reputation: 4310
In the end I decided not to bother with trying to P/Invoke WMI IPHelper functions. It was much easier to just kick-off a process opening cmd.exe and writing the console commands to add and remove route. The code below illustrates how this can be done.
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();
Upvotes: 3