1

I have an issue that I can not figure out. I have a Windows form with a Text Box, Button, and List Box. I want to type an IP into the text box, push the button, and redirect the schtasks output to my list box. However, I never get anything more than the first line. Also, my code works fine when redirecting to a text file. Below is my code.

        string machineName = textBox1.Text;

        Process process = new Process();
        process.StartInfo.FileName = "schtasks";
        process.StartInfo.Arguments = " /query /s " + machineName;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.CreateNoWindow = true;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        lstOutput.Items.Add(output);

My code to write to text file was the same except at the end, instead of writing to the listbox, I created a text writer and gave it a location for the file. Can anyone figure out what I have done wrong?

Matt
  • 1,220
  • 3
  • 21
  • 36
  • Are you sure it didn't work with the listbox? if you did a `string test = lstOutput.Items[lstOutput.Items.Count - 1];` immediately after the last line of the above sample, do you get the whole output back? or is it truncated? I'm just wondering if it might be truncated in the display of it, rather than in the data transfer. – Tim May 17 '11 at 20:20
  • That line gave me an error saying object could not be converted to string. – Matt May 17 '11 at 21:54
  • Sorry, that should have been `string test = lstOutput.Items[lstOutput.Items.Count - 1].ToString();` or `object testObject = lstOutput.Items[lstOutput.Items.Count - 1]; string testString = testObject.ToString();` but it looks like you've got a suitable answer below. – Tim May 18 '11 at 12:38

1 Answers1

3

I think you need to parse the output more to massage it into the list box. I ran your example with a textbox and got all of the output, just like at a command line. I think the list box is just adding all of the output as one item maybe?

Try something like this:

    string[] lines = output.Split('\n');
    foreach (string s in lines)
    {
        lbResult.Items.Add(s);
    }
Decker97
  • 1,643
  • 10
  • 11
  • That worked perfectly. Thank you. Is there anyway to control the output spacing? – Matt May 17 '11 at 21:32
  • If you change the font on the list box to Courier (or another even spaced font) the output will look better. – Decker97 May 18 '11 at 17:23