如何以编程方式打开“网络发现"在 Windows 操作系统中? [英] How to programmatically turn on "Network Discovery" in Windows OS?

查看:52
本文介绍了如何以编程方式打开“网络发现"在 Windows 操作系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用 UPnP 协议打开端口.Windows 默认禁用 UPnP 设备发现,需要在网络和共享中心中打开网络发现以启用 UPnP 设备发现.

My project open ports using UPnP protocol. Windows disables UPnP device discovery by default, one needs to turn on Network Discovery in Network and Sharing Center to enable UPnP device discovery.

有没有办法以编程方式做到这一点?

Is there a way to do this programatically?

推荐答案

可以使用cmd命令启用网络发现

You can use cmd command for enable network discovery

netsh firewall set service type = upnp mode = mode

然后将该命令作为代码的参数提供

then give that command as parameter to code

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
  }
}

如果该命令不起作用,请根据您的系统查找另一个命令以启用网络发现.

If that command doesnt work find another command to enable network discovery acording to your system.

这篇关于如何以编程方式打开“网络发现"在 Windows 操作系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆