0

我将这个 c# asp 代码隐藏在一起,以从服务器中提取 WMI 信息。

提取 WMI 信息时,代码似乎可以工作。

当我将 WMI 数据输出到文本文件时,一切正常。

问题: 当我尝试将 WMI 数据输出到我的 asp/web 表单中的文本框控件时,它只显示最后一个WMI 对象,而不是显示集合中的所有对象。

例如: 我的 WMI 查询从服务器中提取硬盘信息。服务器有 3 个硬盘驱动器(驱动器 C、D、E)。

  • 到文本文件的 WMI 输出显示来自所有 3 个硬盘驱动器(驱动器 C、D、E)的信息。

  • 到我的 texbox Web 控件的 WMI 输出仅显示列表中最后一个硬盘驱动器(驱动器 E)的信息。

我的 foreach 循环有问题吗?我是否为此类项目使用了错误类型的 Web 控件?

我不确定我做错了什么/我不理解什么。

在此先感谢您提供的所有教育和帮助!

当前代码:

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();
    } 
}
4

1 回答 1

2

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();

        } 


    }
于 2013-10-16T15:07:43.763 回答