سؤال

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)

هل كانت مفيدة؟

المحلول

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top