-1

I have a list and want to run each item in ListViewItem through command line. Currently it seems to me that my list is propagating each item with a space before the text which causes Command Line to not recognize the strings as valid data. How can i remove the whitespace at the beginning and end of each string value in my list?

NOTE: You can see how the list is structured in the area notated as 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();
        }
     }
Joe Pearson
  • 149
  • 1
  • 2
  • 13
  • @AdamHeeg I looked for the last 3 days. And know very well about .Trim(), in fact you can see I use Regex in my code. Please don't come here just to insult me. I am simply not seeing something here and am asking a specific question about a specific gap in my understanding of C#. – Joe Pearson Aug 25 '15 at 21:06
  • I don't think the problem is removing white spaces from the string, but the concatenation of a ListViewItem to a string. – Steve Aug 25 '15 at 21:07
  • Please accept my apology. – Adam Heeg Aug 26 '15 at 13:17

2 Answers2

1

In this line

 cmd2.StartInfo.Arguments = "wlan show profiles name=" + item;  

you are concatenating a ListViewItem to a string. This is resolved calling the ToString method of the ListViewItem that produces something like this

 cmd2.StartInfo.Arguments = "wlan show profiles name=ListViewItem: {...whatever...}

of course this is not a valid netsh command.

You need to change it to

 cmd2.StartInfo.Arguments = "wlan show profiles name=" + item.Text.Trim();  

(Trimmin just in case that effectively the Regex keeps some whitespace around)

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    Dude.... I had no idea you could add tring to the end of a placeholder like that. You are absolutely awesome, thank you for reading through the question and understanding the issue before posting some generic comment/ bumping the question. – Joe Pearson Aug 25 '15 at 21:12
0

use Trim();

string stringToTrim = "   asdf    ";
Console.Log(stringToTrim.Trim());

// Output: 'asdf'
maraaaaaaaa
  • 7,749
  • 2
  • 22
  • 37
  • So instead of writing to console log can I simply have these inject directly back into my list? or possibly a second list? Or am i misunderstanding what the outcome is expected to be. – Joe Pearson Aug 25 '15 at 21:08