'Running netsh command in cmd using c# results in restart

So for context, I'm trying to create a program in C#, which starts a CMD prompt and executes the command "netsh wlan show profiles". For this I start a CMD process and redirect the console input (and later output), before using StreamWriter to execute the command.

This works with commands like echo, ipconfig, @echo off, color... and so on. But if I try netsh the process restarts over and over again:

Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\Heinz Mütze\source\repos\netsh\netsh\bin\Debug>netsh wlan show profiles
Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\Heinz Mütze\source\repos\netsh\netsh\bin\Debug>netsh wlan show profiles
Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\Heinz Mütze\source\repos\netsh\netsh\bin\Debug>netsh wlan show profiles
Microsoft Windows [Version 10.0.22000.613]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

and so on...

Anyone knows how I can fix it? Heres my code:

using System.IO;
using System.Diagnostics;
using System;
using System.Linq;
using System.Threading;

namespace netsh
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var p = new Process();
            var startinfo = new ProcessStartInfo("cmd.exe")
            {
                RedirectStandardInput = true,
                RedirectStandardOutput = false,
                UseShellExecute = false
            };
            p.StartInfo = startinfo;
            p.Start();

            using (StreamWriter sw = p.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("netsh wlan show profiles");
                    Thread.Sleep(5000);
                    p.Kill();
                }
            }
//            using (StreamReader sr = p.StandardOutput)
//            {
//                if (sr.BaseStream.CanRead)
//                {
//                    string output;
//                    output = sr.ReadToEnd();
//                    File.WriteAllText(@"C:\Users\Heinz Mütze\Desktop\CMD-Test\netsh.txt", output);
//                }
//            }
        }
    }
}


Solution 1:[1]

I kinda found the problem. I think something messed up the file. I created a new one and copy pasted the code and everything works just fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ender Gamer