开发者

Injecting commands into Command Line based Process

开发者 https://www.devze.com 2023-03-25 12:24 出处:网络
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

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号