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);
// }
// }
}
}
}