🟣 C# / .NET 6+

Backup Processes to JSON (C#)

Snapshots Processes (Win32_Process) to a timestamped JSON file via System.Management and System.Text.Json.

By WindowsScripting.com · 1.2 KB
BackupProcessListToJson.cs
// BackupProcessListToJson.cs
//
// Snapshots Processes (Win32_Process) to a timestamped JSON file.
//
// Requires the System.Management package:
//   dotnet new console -o BackupProcess
//   dotnet add package System.Management
//   (replace the generated Program.cs with this file, then dotnet run)

using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Text.Json;

var searcher = new ManagementObjectSearcher("SELECT Name, ProcessId, WorkingSetSize, ThreadCount FROM Win32_Process");
var results = new List<Dictionary<string, object?>>();

foreach (ManagementObject item in searcher.Get())
{
    var row = new Dictionary<string, object?>();
    row["Name"] = item["Name"];
        row["ProcessId"] = item["ProcessId"];
        row["WorkingSetSize"] = item["WorkingSetSize"];
        row["ThreadCount"] = item["ThreadCount"];
    results.Add(row);
}

string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string outPath = $"process-snapshot_{timestamp}.json";
File.WriteAllText(outPath, JsonSerializer.Serialize(results, new JsonSerializerOptions { WriteIndented = true }));

Console.WriteLine($"Snapshot of Processes ({results.Count} items) saved to {outPath}");