Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a fairly basic program in C#, kind of like a system monitor, which our helpdesk uses in order to quickly get some information from users.

It displays Username, Domain, OS, IP and a few other bits of info.

I have since been asked to extend the functionality to include total RAM and the computer serial number, but no matter what i have tried i cant get it to display in to a text box.

What I have tried:

For the RAM i tried using a performance counter, but this only showed available RAM. So i think i need to use WMI?

For the serial i have this:

C#
public static string GetMotherBoardID()
        {
            string mbInfo = String.Empty;
            ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
            scope.Connect();
            ManagementObject wmiClass = new ManagementObject(scope, new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());

            foreach (PropertyData propData in wmiClass.Properties)
            {
                if (propData.Name == "SerialNumber")
                    mbInfo = String.Format("{0,-25}{1}", propData.Name, Convert.ToString(propData.Value));
            }

            return mbInfo;
            ITHelper.txtSerial.Text = mbInfo.ToString();
        }


Which displays the following error

An object reference is required for the non-static field, method, or property


For the RAM i have tried

C#
private static void DisplayTotalRam()
       {
           string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
           ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
           int amount = 0;
           foreach (ManagementObject memo in searcher.Get())
           {
               amount += Convert.ToInt32(Convert.ToInt64(memo["Capacity"]) / 1024 / 1024 / 1024);
               txtRam.Text = amount.ToString();
           }
       }


But i get the same message as above
Posted
Updated 29-Dec-16 2:49am
Comments
CPallini 29-Dec-16 8:45am    
Which is the offending line?
Member 11982907 29-Dec-16 8:49am    
ITHelper.txtSerial.Text = mbInfo.ToString();

and

txtRam.Text = amount.ToString();

Thanks
Richard MacCutchan 29-Dec-16 11:52am    
You are trying to access fields on your form from static methods.

1 solution

0) Add a reference to Microsoft.VisualBasic

1) Add using DEVICES=Microsoft.VisualBasic.Devices; at the top of your file

2) Do this in your code: DEVICES.ComputerInfo info = new DEVICES.ComputerInfo();

3) Inspect the info object properties to retrieve the desired info.
 
Share this answer
 
v3
Comments
Member 11982907 29-Dec-16 9:05am    
I have tried this and all i get is this

txtRAM.Text = DEVICES.ComputerInfo. and then no other options or info to display

Same as:

txtRam.Text = Microsoft.VisualBasic.Devices.ComputerInfo.

Nothing else is populated, just "Equals"

Sorry im a bit of a noob to all this, so may be missing something obvious.
#realJSOP 29-Dec-16 9:51am    
Being a "noob" doesn't excuse laziness (learn to use google, or pay attention to the intellisense popups). I updated my answer.
Member 11982907 3-Jan-17 3:17am    
Im not being lazy, im completely self taught on this, i have no point of reference and have only been doing it a few months, in between what is my actual job. I have googled these issues to death without finding an answer. I will look into what you have posted though

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