Add steam running games

This commit is contained in:
Din Dang
2024-11-28 19:28:37 +08:00
parent f6ac22ee8d
commit 62e77433aa
28 changed files with 1507 additions and 11 deletions
+46 -2
View File
@@ -31,13 +31,15 @@ namespace ResourceMonitorService
var cpuUsage = GetCpuUsage();
var ramUsage = GetRamUsage();
var gpuUsage = GetGpuUsage();
var runningGame = GetCurrentlyRunningGame();
var resourceUsage = new
{
ComputerInfo = computerInfo,
CPU = cpuUsage,
RAM = ramUsage,
GPU = gpuUsage
GPU = gpuUsage,
CurrentlyRunningGame = runningGame
};
var json = JsonConvert.SerializeObject(resourceUsage);
@@ -111,5 +113,47 @@ namespace ResourceMonitorService
FanSpeed = fanSpeed
};
}
private object GetCurrentlyRunningGame()
{
var processes = Process.GetProcesses();
foreach (var process in processes)
{
try
{
var filePath = process.MainModule.FileName;
if (filePath.Contains(@"\steamapps\common\"))
{
// Extract the game directory name
var parts = filePath.Split(new[] { @"\steamapps\common\" }, StringSplitOptions.None);
if (parts.Length > 1)
{
var gamePath = parts[1];
var gameName = gamePath.Split(Path.DirectorySeparatorChar)[0];
return new
{
GameName = gameName,
ExecutableName = Path.GetFileName(filePath),
FullPath = filePath,
ProcessId = process.Id,
MemoryUsage = process.WorkingSet64 / (1024 * 1024) + " MB", // Memory usage in MB
CpuTime = process.TotalProcessorTime.ToString(),
StartTime = process.StartTime.ToString("G"), // General date/time pattern
UserName = Environment.UserName // The user running the process
};
}
}
}
catch (Exception)
{
// Handle access exceptions or continue if not important
}
}
return "No Steam game is currently running.";
}
}
}
}