how do I execute cmd.exe process only once and in

2019-09-13 01:41发布

问题:

I want to execute process of cmd.exe only once outside foreach and inside foreach want to send parameters to this process.

I am currently doing this:

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

回答1:

If I get you right, it should look something like this:

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