Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear experts ;
i'm work on c# windows application project using entity framework and sql database i need to get the server cpu serial and current date but from the server that have database which i connected not from local pc

What I have tried:

public static string GetMotherBoardID()
        {
            string cpuInfo = string.Empty;
            ManagementClass mc = new ManagementClass("win32_processor");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if (cpuInfo == "")
                {
                    //Get only the first CPU's ID
                    cpuInfo = mo.Properties["processorID"].Value.ToString();
                    break;
                }
            }
            return cpuInfo;
        }
Posted
Updated 12-Sep-18 8:14am
Comments
M4rLask 11-Sep-18 12:51pm    
What is the problem?

1 solution

It looks to me like you are on the right track. It is just a matter of determining which pieces to query.

This can be improved on, but its a start:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.IO;

namespace ConsoleApp2
{
    class Program
    {
        public static bool GetComputerInfo(string machine, string win32object, string attributename)
        {
            bool bReturnValue = true;

            try
            {
                // Use the Storage management scope
                ManagementScope scope = new ManagementScope($@"\\{machine}\ROOT\cimv2");  
                // Define the query
                ObjectQuery query = new ObjectQuery($"SELECT * FROM {win32object}");
                // create the search
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                //get a collection of WMI objects
                ManagementObjectCollection queryCollection = searcher.Get();

                //enumerate the collection.
                foreach (ManagementObject m in queryCollection)
                {
                    // access properties of the WMI object
                    Console.WriteLine($"{attributename} : {m[attributename]}");

                }


            }
            catch(Exception ex)
            {
                Console.WriteLine($"{ex.Message}\n{ex.InnerException}\n{ex.StackTrace}");
                bReturnValue = false;
            }
            return bReturnValue;
        }        

        static void Main(string[] args)
        {
            // could make machine a command line arg
            string sTargetMachine = "localhost";    // <-- put target machine name here

            GetComputerInfo(sTargetMachine, "win32_ComputerSystemProduct", "IdentifyingNumber");
            GetComputerInfo(sTargetMachine, "win32_bios", "SerialNumber");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Year");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Month");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Day");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Hour");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Minute");
            GetComputerInfo(sTargetMachine, "win32_LocalTime", "Second");

            Console.ReadLine();
        }
    }
}



Some other things I found useful while working on this, fire up Powershell

in powershell run the following to get a list of what is out there and queryable

get-wmiobject -List

Then based on what class / object you want to retrieve you can easily look at the available attributes, for example
PS> get-wmiobject win32_ComputerSystemProduct

IdentifyingNumber : XXXXXXX
Name              : Latitude XXXX
Vendor            : Dell Inc.
Version           :
Caption           : Computer System Product
 
Share this answer
 
Comments
littleGreenDude 12-Sep-18 14:51pm    
Additional note - I'm pretty sure you must be logged in as a network admin to get results from a remote machine. Otherwise, I'm guessing you will probably see some sort of RPC error.

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