Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#,如何从批处理脚本创建的新cmd.exe运行命令?_C#_Batch File_Cmd - Fatal编程技术网

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

使用C#,如何从批处理脚本创建的新cmd.exe运行命令?,c#,batch-file,cmd,C#,Batch File,Cmd,我有一个批处理脚本,它设置了一些环境变量,然后在当前执行的cmd.exe中设置了一个新的cmd.exe 批处理脚本的外观示例: setlocal set foo=bar cmd.exe 如何启动一个调用此批处理脚本的新Process()对象,然后将StandardInput导入cmd.exe的新实例 这是我当前无法使用的代码: ProcessStartInfo procStartInfo = new ProcessStartInfo(); procStartInfo.FileName = @"

我有一个批处理脚本,它设置了一些环境变量,然后在当前执行的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
}

您必须使用
ProcessStartInfo
类并设置
RedirectStandardInput
标志,然后提供一个将提供数据的流