c# 将 WMI 输出发送到文本框 Web 控件

本文关键字:文本 Web 控件 WMI 输出 出发 | 更新日期: 2023-09-27 18:35:34

我把这个 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();
    } 
}

c# 将 WMI 输出发送到文本框 Web 控件

发生这种情况的原因是因为你正在写前一个(你只是看不到它,因为它发生得太快了)。

相反,你应该在你的foreach循环之外创建一个变量,(就像我在下面显示的那样)

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

    }