【问题标题】:How do I use a list<string> to loop a process in cmd?如何使用 list<string> 在 cmd 中循环进程?
【发布时间】:2015-08-18 17:01:00
【问题描述】:

我有一个 netsh 分析器,我想获取找到的每个循环的详细信息。

        private void wifiButton_Click(object sender, EventArgs e)
    {


        Process cmd = new Process();
        cmd.StartInfo.FileName = "netsh.exe";
        System.Threading.Thread.Sleep(50);
        cmd.StartInfo.Arguments = "wlan show profiles";
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.RedirectStandardError = true;
        cmd.Start();
        //* Read the output (or the error)
        string output = cmd.StandardOutput.ReadToEnd();
        textBox3.Text = output;
        cmd.WaitForExit();




        // output would be set by earlier code
        var Output = textBox3.Text;

        var regex = new Regex(@"All User Profile[\s]+: (.*)");
        var resultList = new List<string>();

        foreach (Match match in regex.Matches(Output))
        {
            resultList.Add(match.Groups[1].ToString());
        }
        textBox4.Text = string.Join(", ", resultList);

上面的代码有效,下面我要取出列表并再次通过CMD运行结果以获取详细结果。

        for (int i = 0; i < resultList.Count; i++)
        {
        Process cmd2 = new Process();
        cmd.StartInfo.FileName = "netsh.exe";
        System.Threading.Thread.Sleep(50);
        cmd.StartInfo.Arguments = "wlan show profiles name=" resultList(0) + " key=clear";
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.RedirectStandardError = true;
        cmd.Start();
        //* Read the output (or the error)
        string output2 = cmd2.StandardOutput.ReadToEnd();
        textBox5.Text = output2;
        cmd.WaitForExit();
        }
 }

【问题讨论】:

  • 不应该是resultList[i]吗?你遇到了什么错误?是什么阻止了您的代码工作?
  • @David 我可以吻你哈哈,你是完全正确的人。那和我将我的第二组循环错误地标记为 cmd 而不是 cmd2。看起来它大部分都在工作。
  • @code 我用 (0) 而不是 (i)

标签: c# list loops netsh


【解决方案1】:

我很好,用 0 代替了 i 占位符。此外,错误地标记了我的第二次 cmd 运行并忘记更新到 cmd2,因此之前的命令行会重叠并中断。

   private void wifiButton_Click(object sender, EventArgs e)
    {


        Process cmd = new Process();
        cmd.StartInfo.FileName = "netsh.exe";
        System.Threading.Thread.Sleep(50);
        cmd.StartInfo.Arguments = "wlan show profiles";
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.RedirectStandardError = true;
        cmd.Start();
        //* Read the output (or the error)
        string output = cmd.StandardOutput.ReadToEnd();
        textBox3.Text = output;
        cmd.WaitForExit();
        System.Threading.Thread.Sleep(500);


        // output would be set by earlier code
        var Output = textBox3.Text;

        var regex = new Regex(@"All User Profile[\s]+: (.*)");
        var resultList = new List<string>();

        foreach (Match match in regex.Matches(Output))
        {
            resultList.Add(match.Groups[1].ToString());
        }
        textBox4.Text = string.Join(", ", resultList);


        System.Threading.Thread.Sleep(500);

        for (int i = 0; i < resultList.Count; i++)
        {
            Process cmd2 = new Process();
            cmd2.StartInfo.FileName = "netsh.exe";
            System.Threading.Thread.Sleep(50);
            cmd2.StartInfo.Arguments = "wlan show profiles name=" + resultList[i] + " key=clear";
            cmd2.StartInfo.UseShellExecute = false;
            cmd2.StartInfo.RedirectStandardOutput = true;
            cmd2.StartInfo.RedirectStandardError = true;
            cmd2.Start();
            //* Read the output (or the error)
            string output2 = cmd2.StandardOutput.ReadToEnd();
            textBox5.Text = output2;
            cmd2.WaitForExit();
        }
}

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多