在我的WPF应用程序中,我试图通过System.Diagnostics.Process打开cmd.exe,但每次遇到process.Start()它立即关闭,我不能写任何其他内容。但是,如果我调用静态Process.Start()它将保持打开但我不确定如何写入它。见下文。
var processInfo = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
};
var process = new Process()
{
StartInfo = processInfo,
};
process.Start(); // This close immediately and not work
Process.Start("cmd.exe"); // This will work but can't write to it
process.StandardInput.WriteLine(someText);
process.StandardInput.WriteLine(moreText);
答案 0 :(得分:1)
用于等待cmd。
process.WaitForExit();
答案 1 :(得分:0)
我做了一些研究,发现实施的解决方案Here有效。不确定这是否是最佳方法,但它对我有用。
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
private void BtnSubmit_OnClick(object sender, RoutedEventArgs e)
{
var processInfo = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true
};
var process = new Process()
{
StartInfo = processInfo,
};
AllocConsole();
process.Start(); // This close immediately and not work
process.StandardInput.WriteLine("someText");
process.StandardInput.WriteLine("moreText");
process.WaitForExit();
}