user2655078
user2655078

Reputation: 1

c# send WMI output to a textbox web control

I put together this c# asp code-behind to pull WMI information from servers.

When pulling the WMI information, the code seems to work.

When I output the WMI data to a text file, everything works.

Problem: When I try to output the WMI data into a textbox control in my asp/web form, it only displays the very last WMI object, instead of displaying all objects in the collection.

For example: my WMI query pulls hard drive information from a server. The server has 3 hard drives (drives C, D, E).

Is there something wrong with my foreach loop? Am I using the wrong type of web control for this type of project?

I’m not sure what I’m doing wrong/what I'm not understanding.

Thank you in advance for any and all education and help with this!

Current code:

public void getWMIdata(string query)
{
    ConnectionOptions co = new ConnectionOptions();
    co.EnablePrivileges = true;
    co.Impersonation = ImpersonationLevel.Impersonate;
    co.Username = TextBox2_userID.Text;
    co.Password = TextBox3_password.Text;

    string host = TextBox1_serverName.Text;

    string wmiNameSpace = @"root\cimv2";
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\{1}", host, wmiNameSpace), co);

    try
    { 
        ObjectQuery objquery = new ObjectQuery(query);       
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objquery);
        ManagementObjectCollection queryCollection = searcher.Get();

        foreach (ManagementObject queryObj in queryCollection)
        {
            ////convert free disk space size from bytes to GB's
            double fdsbytes = Convert.ToDouble(queryObj["FreeSpace"].ToString());
            double fdsGB = Math.Round((fdsbytes / Math.Pow(1024, 3)), 2);
            string fdsFinal = @"Free Disk Space: " + Convert.ToString(fdsGB) + @"GB";

            ////convert total disk drive size from bytes to GB's
            double dsbytes = Convert.ToDouble(queryObj["Size"].ToString());
            double dsGB = Math.Round((dsbytes / Math.Pow(1024, 3)), 2);
            string dsFinal = @"Disk Drive Size: " + Convert.ToString(dsGB) + @"GB";

            ////% free disk space
            double a = Math.Round((100 * (fdsGB / dsGB)), 2);
            string percentfreespace = @"% Free Space: " + Convert.ToString(a) + @"GB";

            string name = @"Drive Name: " + queryObj["Name"].ToString();

            string description = @"Drive Description: " + queryObj["Description"].ToString();

            TextBox1_wmiOutput.Text = fdsFinal + Environment.NewLine + dsFinal + Environment.NewLine + percentfreespace + Environment.NewLine + name + Environment.NewLine + description + Environment.NewLine + Environment.NewLine;
        } 
    }
    catch (ManagementException ex)
    {
        Label3_wmiErrorMesage.Text = @"Error Message: " + ex.Message.ToString();
    } 
}

Upvotes: 0

Views: 820

Answers (1)

gleng
gleng

Reputation: 6304

The reason this is happening is because you are are writing over the previous one (you just can't see it because it's happening too fast).

Instead, you should make a variable outside of your foreach loop, (like i've shown below)

public void getWMIdata(string query)
    {
        ConnectionOptions co = new ConnectionOptions();
        co.EnablePrivileges = true;
        co.Impersonation = ImpersonationLevel.Impersonate;
        co.Username = TextBox2_userID.Text;
        co.Password = TextBox3_password.Text;

        string host = TextBox1_serverName.Text;

        string wmiNameSpace = @"root\cimv2";
        ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\{1}", host, wmiNameSpace), co);

        try
        { 

            ObjectQuery objquery = new ObjectQuery(query);       
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objquery);
            ManagementObjectCollection queryCollection = searcher.Get();

            string displayedText = ""; // Here

            foreach (ManagementObject queryObj in queryCollection)
            {

                ////convert free disk space size from bytes to GB's
                double fdsbytes = Convert.ToDouble(queryObj["FreeSpace"].ToString());
                double fdsGB = Math.Round((fdsbytes / Math.Pow(1024, 3)), 2);
                string fdsFinal = @"Free Disk Space: " + Convert.ToString(fdsGB) + @"GB";

                ////convert total disk drive size from bytes to GB's
                double dsbytes = Convert.ToDouble(queryObj["Size"].ToString());
                double dsGB = Math.Round((dsbytes / Math.Pow(1024, 3)), 2);
                string dsFinal = @"Disk Drive Size: " + Convert.ToString(dsGB) + @"GB";

                ////% free disk space
                double a = Math.Round((100 * (fdsGB / dsGB)), 2);
                string percentfreespace = @"% Free Space: " + Convert.ToString(a) + @"GB";

                string name = @"Drive Name: " + queryObj["Name"].ToString();

                string description = @"Drive Description: " + queryObj["Description"].ToString();

                // add this to the text variable to be displayed

                displayedText += fdsFinal + Environment.NewLine + dsFinal + Environment.NewLine + percentfreespace + Environment.NewLine + name + Environment.NewLine + description + Environment.NewLine + Environment.NewLine;

            } 

            // set text to be displayed

            TextBox1_wmiOutput.Text = displayedText;

        }
        catch (ManagementException ex)
        {
            Label3_wmiErrorMesage.Text = @"Error Message: " + ex.Message.ToString();

        } 


    }

Upvotes: 2

Related Questions