我的项目使用 UPnP 协议(protocol)打开端口。 Windows 默认禁用 UPnP 设备发现,需要在 网络和共享中心 中打开 网络发现 才能启用 UPnP 设备发现。
有没有办法以编程方式做到这一点?
最佳答案
您可以使用 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
}
}
如果该命令不起作用,请查找另一个命令以根据您的系统启用网络发现。
关于c# - 如何在 Windows 操作系统中以编程方式打开 "Network Discovery"?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8322177/