4

私のプロジェクトはUPnPプロトコルを使用してポートを開きます。WindowsはデフォルトでUPnPデバイス検出を無効にします。UPnPデバイス検出を有効にするには、ネットワークおよび共有センターでネットワーク検出をオンにする必要があります。

これをプログラムで行う方法はありますか?

4

1 に答える 1

11

cmdコマンドを使用して、ネットワーク検出を有効にすることができます

netsh firewall set service type = upnp mode = mode

次に、そのコマンドをコードのパラメーターとして指定します

public void ExecuteCommandSync(object command)
{
  try
  {
    // create the ProcessStartInfo using "cmd" as the program to be run,
    // and "/c " as the parameters.
    // Incidentally, /c tells cmd that we want it to execute the command that follows,
    // and then exit.
    System.Diagnostics.ProcessStartInfo procStartInfo =
      new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    // The following commands are needed to redirect the standard output.
    // This means that it will be redirected to the Process.StandardOutput StreamReader.
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    // Do not create the black window.
    procStartInfo.CreateNoWindow = true;
    // Now we create a process, assign its ProcessStartInfo and start it
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    // Get the output into a string
    string result = proc.StandardOutput.ReadToEnd();
    // Display the command output.
    Console.WriteLine(result);
  }
  catch (Exception objException)
  {
    // Log the exception
  }
}

そのコマンドが機能しない場合は、システムに応じてネットワーク検出を有効にする別のコマンドを見つけてください。

于 2011-11-30T08:33:00.583 に答える