Created
May 4, 2023 12:28
-
-
Save junaid109/6550ce42089c84be4b7106c15ac94d36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Management; | |
namespace DeviceStatsLogger | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Retrieving device stats..."); | |
// Query for CPU information | |
var cpuQuery = new ObjectQuery("SELECT * FROM Win32_Processor"); | |
using (var searcher = new ManagementObjectSearcher(cpuQuery)) | |
{ | |
foreach (var result in searcher.Get()) | |
{ | |
var name = result["Name"]; | |
var numCores = result["NumberOfCores"]; | |
var clockSpeed = result["MaxClockSpeed"]; | |
Console.WriteLine($"CPU: {name} ({numCores} cores, {clockSpeed} MHz)"); | |
} | |
} | |
// Query for GPU information | |
var gpuQuery = new ObjectQuery("SELECT * FROM Win32_VideoController"); | |
using (var searcher = new ManagementObjectSearcher(gpuQuery)) | |
{ | |
foreach (var result in searcher.Get()) | |
{ | |
var name = result["Name"]; | |
var driverVersion = result["DriverVersion"]; | |
Console.WriteLine($"GPU: {name} (Driver version: {driverVersion})"); | |
} | |
} | |
Console.WriteLine("Device stats retrieved."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Timers; | |
namespace DeviceStatusLogger | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Timer timer = new Timer(); | |
timer.Elapsed += new ElapsedEventHandler(LogDeviceStatus); | |
timer.Interval = 10000; // log every 10 seconds | |
timer.Enabled = true; | |
Console.WriteLine("Press any key to exit."); | |
Console.ReadKey(); | |
} | |
static void LogDeviceStatus(object source, ElapsedEventArgs e) | |
{ | |
float cpuUsage = GetCpuUsage(); | |
float gpuUsage = GetGpuUsage(); | |
float memoryUsage = GetMemoryUsage(); | |
float diskUsage = GetDiskUsage(); | |
string logMessage = $"{DateTime.Now.ToString()} - CPU usage: {cpuUsage}% - GPU usage: {gpuUsage}% - Memory usage: {memoryUsage} MB - Disk usage: {diskUsage} MB"; | |
Console.WriteLine(logMessage); | |
string logFilePath = "device_status.log"; | |
using (StreamWriter sw = File.AppendText(logFilePath)) | |
{ | |
sw.WriteLine(logMessage); | |
} | |
} | |
static float GetCpuUsage() | |
{ | |
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); | |
cpuCounter.NextValue(); | |
System.Threading.Thread.Sleep(1000); // wait for 1 second | |
return cpuCounter.NextValue(); | |
} | |
static float GetGpuUsage() | |
{ | |
PerformanceCounter gpuCounter = new PerformanceCounter("GPU Engine", "% Utilization", "_Total"); | |
gpuCounter.NextValue(); | |
System.Threading.Thread.Sleep(1000); // wait for 1 second | |
return gpuCounter.NextValue(); | |
} | |
static float GetMemoryUsage() | |
{ | |
PerformanceCounter memoryCounter = new PerformanceCounter("Memory", "Available MBytes"); | |
return (float)(new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory - (memoryCounter.NextValue() * 1024 * 1024)) / (1024 * 1024); | |
} | |
static float GetDiskUsage() | |
{ | |
PerformanceCounter diskCounter = new PerformanceCounter("LogicalDisk", "Free Megabytes", "_Total"); | |
return diskCounter.NextValue(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To have a console app running as a background service, you can create a Windows Service using the .NET Framework. Here's an example of how to do this: | |
Create a new C# console application project in Visual Studio. | |
Right-click the project in Solution Explorer, and select "Add -> New Item". | |
Select "Windows Service" from the list of project templates, and name the service something meaningful, like "DeviceStatusService". | |
In the Service1.cs file that is created, rename the Service1 class to DeviceStatusService. | |
Add the following code to the OnStart() method of the DeviceStatusService class: | |
csharp | |
Copy code | |
protected override void OnStart(string[] args) | |
{ | |
// Create a new thread to run the device status check code | |
Thread deviceStatusThread = new Thread(new ThreadStart(DeviceStatusCheck)); | |
deviceStatusThread.Start(); | |
} | |
Add the following code to the DeviceStatusCheck() method: | |
csharp | |
Copy code | |
private void DeviceStatusCheck() | |
{ | |
while (true) | |
{ | |
// Get CPU and GPU usage information | |
PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); | |
PerformanceCounter gpuCounter = new PerformanceCounter("GPU Engine", "Utilization Percentage", "_Total"); | |
float cpuUsage = cpuCounter.NextValue(); | |
float gpuUsage = gpuCounter.NextValue(); | |
// Log the information to a file or database | |
string logMessage = string.Format("CPU Usage: {0}%, GPU Usage: {1}%", cpuUsage, gpuUsage); | |
// replace this line with your own logging code | |
// Wait for 5 seconds before checking again | |
Thread.Sleep(5000); | |
} | |
} | |
In the OnStop() method of the DeviceStatusService class, add any cleanup code you need to stop the service gracefully. | |
csharp | |
Copy code | |
protected override void OnStop() | |
{ | |
// Stop the device status check thread | |
// Add any cleanup code you need here | |
} | |
Build the solution, then install the service by opening a command prompt as Administrator and running the following command: | |
lua | |
Copy code | |
sc.exe create DeviceStatusService binPath= "path\to\your\service\exe" | |
Replace path\to\your\service\exe with the path to your compiled .exe file. | |
Start the service using the Services console in Windows, or by running the following command: | |
sql | |
Copy code | |
sc.exe start DeviceStatusService | |
Now your console app will run in the background as a Windows service, continuously logging the device status to your chosen output. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment