llk
llk

Reputation: 2561

Listing WMI data in a textbox with multiple lines of input

I am trying to use WMI to gather system data which in this case I am grabbing all the startup programs and displaying them in a richtextbox. When I run the code, it works but the problem is each time it just overwrites the current text in the box and only ends up displaying the last startup item instead of all 20 startup items. Here is my code :

ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_StartupCommand");

        foreach (ManagementObject queryObj in searcher.Get())
        {

            richTextBox1.Text = "Location: {0}" + queryObj["Location"];
            richTextBox2.Text = "Location: {0}" + queryObj["Command"];
            richTextBox3.Text = "Location: {0}" + queryObj["Description"];
        }

For example, items A B and C are told to startup. When I run my program, it will only show C in the text box because A and B were previously displayed but each time it just erases it and ends up displaying C because it's last.

Upvotes: 0

Views: 395

Answers (1)

RRUZ
RRUZ

Reputation: 136391

you are replacing the content of the richTextBox instead of addin new lines

try this

richTextBox1.Text += string.Format("Location: {0} \n",queryObj["Location"]);
richTextBox2.Text += string.Format("Command: {0} \n",queryObj["Command"]);
richTextBox3.Text += string.Format("Description: {0} \n",queryObj["Description"]);

or

richTextBox1.AppendText(string.Format("Location: {0} \n",queryObj["Location"]));
richTextBox2.AppendText(string.Format("Command: {0} \n",queryObj["Command"]));
richTextBox3.AppendText(string.Format("Description: {0} \n",queryObj["Description"]));

Upvotes: 2

Related Questions