【发布时间】:2015-08-26 05:09:37
【问题描述】:
我有一个列表,想通过命令行运行 ListViewItem 中的每个项目。目前在我看来,我的列表正在传播每个项目,文本前有一个空格,这导致命令行无法将字符串识别为有效数据。如何删除列表中每个字符串值开头和结尾的空格?
注意:您可以在标记为 STAGE2 的区域中查看列表的结构
private void wifiButton_Click(object sender, EventArgs e)
{
// STAGE 1 -- Query wifi profiles saved on device and isolate SSIDs
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(50);
// output would be set by earlier code
// STAGE 2 remove extra data in string down to the SSID name, then add insividual results into a list
var regex = new Regex(@" User Profile\s+:(.*)");
var resultList = new List<string>();
foreach (Match match in regex.Matches(output))
{
output = string.Concat(output.Split(' '));
resultList.Add(match.Groups[1].ToString());
textBox4.Items.Add(match.Groups[1].ToString());
}
System.Threading.Thread.Sleep(500);
// STAGE 3 For each item in the list created in stage 2, run individual SSID name through netsh and add results to textbox5.
foreach (ListViewItem item in textBox4.Items)
{
//arg = arg.Remove(0, 15);
Process cmd2 = new Process();
cmd2.StartInfo.FileName = "netsh.exe";
cmd2.StartInfo.Arguments = "wlan show profiles name=" + item;
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();
}
}
【问题讨论】:
-
@AdamHeeg 我找了过去 3 天。并且非常了解 .Trim(),事实上你可以看到我在我的代码中使用了 Regex。请不要来这里只是为了侮辱我。我只是在这里没有看到任何东西,而是在问一个关于我对 C# 的理解存在特定差距的特定问题。
-
我认为问题不是从字符串中删除空格,而是将 ListViewItem 连接到字符串。
-
请接受我的道歉。
标签: c# listview cmd whitespace