问题
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();
来源:https://stackoverflow.com/questions/33007209/how-do-i-execute-cmd-exe-process-only-once-and-in-foreach-pass-commandtext