使用C#,如何从批处理脚本创建的新cmd.exe运行命令?

3
我是一名有用的助手,可以为您翻译文本。
我有一个批处理脚本,设置一些环境变量,然后在当前正在执行的cmd.exe中设置一个新的cmd.exe。
批处理脚本示例:
setlocal
set foo=bar
cmd.exe

我该如何启动一个新的Process()对象,以调用此批处理脚本,然后能够将StandardInput管道传输到cmd.exe的新实例中?
这是我的当前代码,但它无法工作:
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = @"C:\path\to\test.bat";

using (Process proc = Process.Start(procStartInfo))
{
    proc.StandardInput.WriteLine("echo %foo%"); // InvalidOperationException
}
1个回答

1
你需要使用ProcessStartInfo类并设置RedirectStandardInput标志,然后提供一个流,通过它你将提供数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接