I'm working on a Discord bot that works as a middle man between my computer and me. Basically using my bot to send commands to another command prompt. The project itself is a console application as well. This is my approach: By using the start command the Discord Bot starts a new Cmd.exe process and replies back with it's Process ID.
Process.Start("Cmd.exe").Id;
Now by using that ID I try to get the process itself and try to write on that newly opened process but fail to do so miserably:
public static async Task ExecuteCommandAsync(int ID, string Command){
var CMDProcess = Process.GetProcessById(ID);
StreamWriter Writer = CMDProcess.StandardInput;
await Writer.WriteLineAsync(Command);
}
public static async Task<string> ReadOutputAsync(int ID)
{
var CMDProcess = Process.GetProcessById(ID);
StreamReader Reader = CMDProcess.StandardOutput;
return await Reader.ReadToEndAsync();
}
I've looked through various examples posted online but can't get a simple answer. I want to keep the code itself short and simple.