Currently I am working on a server manager. The server is a command line application. The server is ran through the Pr开发者_StackOverflow中文版ocess class, and the window is hidden. I've been trying to figure out a way to inject commands into it (save-all), without showing the server window. I have tried to use StreamWriter and the Process's Standard Input, but that didn't work. Does anyone know how to get this to work?
To be able to redirect any of the standard I/O streams of a process, you need to disable shell execute. You probably didn't. You should be able to do it like this:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Environment.GetEnvironmentVariable("COMSPEC"),
RedirectStandardInput = true,
UseShellExecute = false,
},
};
if (process.Start())
{
process.StandardInput.WriteLine("echo FOOBAR!");
process.WaitForExit(3000);
process.StandardInput.WriteLine("exit");
process.WaitForExit();
}
精彩评论