0

サーバーから WMI 情報を取得するために、この c# asp コード ビハインドをまとめました。

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

これが発生する理由は、前のものを上書きしているためです (処理が速すぎて見えないだけです)。

代わりに、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();

        } 


    }
于 2013-10-16T15:07:43.763 に答える