如何只执行一次 cmd.exe 进程并在 foreach 中传递 commandText?

how do I execute cmd.exe process only once and in foreach pass commandText?

我只想在 foreach 外部执行 cmd.exe 进程一次,在 foreach 内部想向该进程发送参数。

我目前正在这样做:

var msbuildPath = (string) regKey.GetValue("MSBuildToolsPath");
foreach (var item in listBox1.Items)
{
    var FilePath = item.ToString();         
    var startInfo = new ProcessStartInfo()
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        Arguments = String.Format("\"{0}\" /nologo ", FilePath),
        FileName = Path.Combine(msbuildPath, "msbuild.exe")
    };

    var proc = Process.Start(startInfo);
    proc.WaitForExit();
}   

如果我没看错,它应该是这样的:

var msbuildPath = (string)regKey.GetValue("MSBuildToolsPath");
StringBuilder sb = new StringBuilder();
foreach (var item in listBox1.Items)
{
   sb.AppendFormat("\"{0}\" ", item);
}
var startInfo = new ProcessStartInfo()
{
   WindowStyle = ProcessWindowStyle.Hidden,
   Arguments = sb.ToString() + " /nologo",
   FileName = Path.Combine(msbuildPath, "msbuild.exe")
};
var proc = Process.Start(startInfo);
proc.WaitForExit();