Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need help in C# with getting individual memory values from Win32_PhysicalMemory into separate text boxes with singe line

C#
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);
         }
      }
}


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

C#
#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

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"
Posted
Updated 3-Sep-13 3:35am
v8
Comments
[no name] 1-Sep-13 20:11pm    
First, please update your question with the actual code you are using.
Second, "I want to place the into separate text boxes", place the what?
Third, please tell us what it is that you are trying to do. Pretty much what you have now is "My code is doing exactly what I told it do and I don't want it to.".
Sergey Alexandrovich Kryukov 2-Sep-13 10:51am    
This is not your actual code! I don't believe you can have the code with "GetProprotyValue" and ".text" compiled.
You are already confused enough, so no need to add confusion by typing the code which you did not actually use. After all, do you need help to use the clipboard?
—SA
JJ1989 2-Sep-13 19:29pm    
GetProprotyValue with .ToString() at the end :-) Test it. The clipboard thing I had deleted the code before coming here so I wrote it in the fly in my question.
JJ1989 2-Sep-13 22:26pm    
don't do what such things anymore?
Sergey Alexandrovich Kryukov 3-Sep-13 0:12am    
You still have first code block with wrong spelling, and another one with good spelling. Please, stop it. Finally, make a clear question with actual code.
—SA

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:
C#
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;
        }
    }
 
Share this answer
 
C#
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);
               }
           }

       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900