2015-08-18 5 views
-1

У меня есть профайлер netsh, который я хочу получить подробную информацию для каждого найденного цикла.Как использовать список <string> для цикла процесса в cmd?

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

Не должно быть 'resultList [i]'? Какую ошибку вы получаете? Что мешает вашему коду работать? – David

+1

В чем проблема? – code

+0

@ Давид Я мог бы поцеловать тебя, ха-ха, ты совершенно прав. Это и я неправильно обозначили свой второй набор для цикла как cmd, а не cmd2. Похоже, он работает по большей части. –

ответ

0

Im хороший и использованный 0 вместо i placeholder. Кроме того, промаркировался мой второй запуск 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(); 
     } 
}