C#ManagementObject wmi帮助。从Win32_PhysicalMemory获取单个内存值到单独的文本框中 [英] C# ManagementObject wmi help. Getting individual memory Value from Win32_PhysicalMemory into separate text boxes

查看:358
本文介绍了C#ManagementObject wmi帮助。从Win32_PhysicalMemory获取单个内存值到单独的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要C#中的帮助,将Win32_PhysicalMemory中的单个内存值放入带有单行的单独文本框中



I need help in C# with getting individual memory values from Win32_PhysicalMemory into separate text boxes with singe line

public void MEMInfo()
{
      ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2",
 "Win32_PhysicalMemory");

      foreach(ManagementObject wmi in mos.Get())
      {
         try
         {
             this.Mem1Tag.text = wmi.GetPropertyValue("Tag").ToString(); //textbox1
             this.Mem2Tag.text = wmi.GetPropertyValue("Tag").ToString(); //textbox2
             this.Mem3Tag.text = wmi.GetPropertyValue("Tag").ToString(); //textbox3
             this.Mem4Tag.text = wmi.GetPropertyValue("Tag").ToString(); //textbox4
         }
         catch (Exception e)
         {
             MessageBox.Show(e.Message);
         }
      }
}





我得到的是:

textbox1:物理内存3

textbox2:物理内存3

textbox3:物理内存3

textbox4:物理内存3



我知道怎么用一个带多条线的文本框来做这个,但是我想把Memory值放在单独的文本框中,这样它的外观就好了。



textbox1:物理内存0

textbox2:物理内存1

textbox3:物理内存2

textbox4:物理内存3





All I get is:
textbox1: Physical Memory 3
textbox2: Physical Memory 3
textbox3: Physical Memory 3
textbox4: Physical Memory 3

I know how to do this with one textbox with mutiline but I want to place the Memory values into separate text boxes so that it appearers like.

textbox1: Physical Memory 0
textbox2: Physical Memory 1
textbox3: Physical Memory 2
textbox4: Physical Memory 3

#region CPUInfo()
        public void CPUInfo()
        {
            mos = new ManagementObjectSearcher(scope, query + obj[2]);



            foreach(ManagementObject wmi in mos.Get())
            {
                try
                {
                    this.CPUman.Text = wmi.GetPropertyValue(value[0]).ToString();
                    this.CPUmod.Text = wmi.GetPropertyValue(value[1]).ToString();
                    this.CPUcore.Text = wmi.GetPropertyValue(value[2]).ToString();
                    this.CPUthreads.Text = wmi.GetPropertyValue(value[3]).ToString();
                    this.cacheL2.Text = CPUcore.Text.ToString() + " x " + wmi.GetPropertyValue(value[4]).ToString() + " KB";
                    this.cacheL3.Text = CPUcore.Text.ToString() + " x " + wmi.GetPropertyValue(value[5]).ToString() + " KB";
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

            if(CPUthreads.Text != CPUcore.Text)
            {
                ThredingB.Checked = true;
            
            }
            else
            {
                ThredingB.Checked = false;
            }


        }
        #endregion



这是因为我编写它而起作用的CPU代码:-)。我不需要或希望得到帮助,我对此感到困惑。我只是打了一个砖墙,谷歌无法帮助我。

scope =root \\CIMV2

query =SELECT * FROM

obj [6] =Win32_PhysicalMemory


This is the CPU code that dose work because I wrote it :-). I don't need or want help from who this I am "confused". I'v just hit a brick wall that Google can't seam to help me with.
scope = "root\\CIMV2"
query = "SELECT * FROM "
obj[6] = "Win32_PhysicalMemory"

推荐答案

如果您正在使用循环,那么您需要做的是设置通过它同时获取TextBox的数组或列表。

沿着这些方向:

If you are using a loop, then what you need to do is set up an array or list of your TextBoxes and index through that at the same time.
Along these lines:
string[] data = "these;are;separate;words".Split(';');
List<TextBox> boxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4 };
int i = 0;
foreach (string s in data)
    {
    if (i < boxes.Count)
        {
        boxes[i++].Text = s;
        }
    }


public void MEMInfo()
       {
           mos = new ManagementObjectSearcher(scope, query + obj[4]);
           int i = 1;

           foreach(ManagementObject wmi in mos.Get())
           {
               try
               {

                   if (i == 1)
                   {
                       this.Mem1Tag.Text = wmi.GetPropertyValue("Tag").ToString();
                       i++;
                   }
                   else if (i == 2)
                   {
                       this.Mem2Tag.Text = wmi.GetPropertyValue("Tag").ToString();
                       i++;
                   }
                   else if (i == 3)
                   {
                       this.Mem3Tag.Text = wmi.GetPropertyValue("Tag").ToString();
                       i++;
                   }
                   else if (i == 4)
                   {
                       this.Mem4Tag.Text = wmi.GetPropertyValue("Tag").ToString();
                       i++;
                   }

               }
               catch (Exception e)
               {
                   MessageBox.Show(e.Message);
               }
           }

       }


这篇关于C#ManagementObject wmi帮助。从Win32_PhysicalMemory获取单个内存值到单独的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆