How to programmatically enable Network Discovery in Windows?

My project opens ports using UPnP protocol. Windows disables UPnP device discovery by default; to enable UPnP device discovery, you must enable Network Discovery Network and Sharing Center .

Is there any way to do this programmatically?

+4
source share
1 answer

You can use cmd command to enable network discovery

netsh firewall set service type = upnp mode = mode 

then pass this 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 this command does not work, find another command to enable network discovery according to your system.

+11
source

Source: https://habr.com/ru/post/1383803/


All Articles