1

Using c# I want to get OS name, disk space, CPU usage of the connected machine to my server. I can get connected machine name by using the following code

foreach (DirectoryEntry computers in root.Children)
{
   foreach (DirectoryEntry computer in computers.Children)
   {                 
     if (computer.Name != "Schema")
     {                           
       string value = computer.Name;                    
     }
   }
}

But don't know to get OS name, disk space, CPU usage of connected machines

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
farrukh aziz
  • 162
  • 1
  • 2
  • 9
  • 2
    an alternative for your consideration - powershell: https://blogs.technet.microsoft.com/askds/2010/02/04/inventorying-computers-with-ad-powershell/ – jazb Oct 25 '18 at 05:07

1 Answers1

0

OS name

Add a .NET reference to Microsoft.VisualBasic. Then call:

new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName

From MSDN:

This property returns detailed information about the operating system name if Windows Management Instrumentation (WMI) is installed on the computer. Otherwise, this property returns the same string as the My.Computer.Info.OSPlatform property, which provides less detailed information than WMI can provide.information than WMI can provide.

Disk space

    private  long GetTotalFreeSpace(string driveName)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady && drive.Name == driveName)
            {
                return drive.TotalSize;
            }
        }
        return -1;
    }

CPU usage

You can use the PerformanceCounter class from System.Diagnostics.

Initialize like this:

PerformanceCounter cpuCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

Consume like this:

public string getCurrentCpuUsage(){
            return cpuCounter.NextValue()+"%";
}

Referances

How to get OS name?

How to get disk space?

How to get CPU usage?

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44