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

扶醉桌前 提交于 2020-01-25 20:57:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!