3

How can i get the Server configuration like System Name, SERVER Name, CPU, Physical memory, commit memory, last boot etc.,

Thanks in advance,

Prasad

Vara Prasad.M
  • 1,530
  • 9
  • 31
  • 55

2 Answers2

4

System.Environment and System.Diagnostics.Process.GetCurrentProcess() offer good starting points for some of this information (although the second is more about the current process, rather than system wide).

For things like memory counts and CPU usage you will likely want to use Peformance Counters.

The topic Using System Monitoring Components on MSDN offers a good intro into how to do this stuff.

For boot times you might find this other SO useful: Get the date-time of last windows shutdown event using .NET

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • How can we get the server last login, last shut down? – Vara Prasad.M Apr 03 '12 at 09:31
  • for the last shutdown - did you read the answers on the other SO I link to? I'm not going to copy and paste everything from that question into this answer - that's not the done thing. – Andras Zoltan Apr 03 '12 at 09:37
  • for the local system i am able to get the last time shut down but for the server last time shut down how can i get? – Vara Prasad.M Apr 03 '12 at 09:49
  • then read the registry remotely - supply a different machine name when opening the registry key. You'll need permissions to do that. – Andras Zoltan Apr 03 '12 at 09:53
3

If you want to get info from remote server you can use smth like this:

        string query = "SELECT * FROM Win32_OperatingSystem";           
        ConnectionOptions connection = new ConnectionOptions();
        if (machineName != "127.0.0.1") // if it is localhost an error'll occur
        {
            connection.Username = admLogon;
            connection.Password = admPassword;
        }
        connection.EnablePrivileges = true;
        connection.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope managementScope = new ManagementScope(@"\\" + machineName + @"\root\CIMV2", connection);
        managementScope.Connect();

        ObjectQuery queryObj = new ObjectQuery(query);
        ManagementObjectSearcher searcher = new   ManagementObjectSearcher(managementScope, queryObj);

        foreach (ManagementObject managementObj in searcher.Get())
        {
           string osName = managementObj.Properties["Caption"].Value.ToString();
           string systemName = managementObj.Properties["csname"].Value;
           string osVersion = managementObj.Properties["Version"].Value;
           string manufacturer = managementObj.Properties["Manufacturer"].Value;
           //etc
        }
thezar
  • 1,278
  • 13
  • 17
  • Here what is the machinename i have to give? – Vara Prasad.M Apr 03 '12 at 09:18
  • @VaraPrasad.M seriously? How can we know what machines you have in your environment? – Andras Zoltan Apr 03 '12 at 09:38
  • machinename can be its ip or full name like myserver.example.com – thezar Apr 03 '12 at 12:07
  • I am using the following code to get the CPU usage PerformanceCounter myCounter; myCounter = new PerformanceCounter(); myCounter.CategoryName = "Processor"; myCounter.CounterName = "% Processor Time"; myCounter.InstanceName = "_Total"; for(int i=0; i < 20; i++) myCounter.NextValue(); I ran this progam i am seeing values 0 or 100%, but when i see the task manager the CPU usage is 6 to 22 %. Am i missing something – Vara Prasad.M Apr 05 '12 at 09:32
  • I hope, this article can help you: http://blogs.msdn.com/b/bclteam/archive/2006/06/02/618156.aspx – thezar Apr 06 '12 at 15:10
  • @thezar The current URL is https://learn.microsoft.com/en-us/archive/blogs/bclteam/how-to-read-performance-counters-ryan-byington – Michel de Ruiter Aug 15 '22 at 09:04