🟣 C# / .NET 6+
Backup Services to JSON (C#)
Snapshots Services (Win32_Service) to a timestamped JSON file via System.Management and System.Text.Json.
BackupServiceListToJson.cs
// BackupServiceListToJson.cs
//
// Snapshots Services (Win32_Service) to a timestamped JSON file.
//
// Requires the System.Management package:
// dotnet new console -o BackupService
// 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, DisplayName, State, StartMode, Status FROM Win32_Service");
var results = new List<Dictionary<string, object?>>();
foreach (ManagementObject item in searcher.Get())
{
var row = new Dictionary<string, object?>();
row["Name"] = item["Name"];
row["DisplayName"] = item["DisplayName"];
row["State"] = item["State"];
row["StartMode"] = item["StartMode"];
row["Status"] = item["Status"];
results.Add(row);
}
string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string outPath = $"service-snapshot_{timestamp}.json";
File.WriteAllText(outPath, JsonSerializer.Serialize(results, new JsonSerializerOptions { WriteIndented = true }));
Console.WriteLine($"Snapshot of Services ({results.Count} items) saved to {outPath}");