// BackupPrinterListToJson.cs // // Snapshots Printers (Win32_Printer) to a timestamped JSON file. // // Requires the System.Management package: // dotnet new console -o BackupPrinter // 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, DriverName, PortName, PrinterStatus FROM Win32_Printer"); var results = new List>(); foreach (ManagementObject item in searcher.Get()) { var row = new Dictionary(); row["Name"] = item["Name"]; row["DriverName"] = item["DriverName"]; row["PortName"] = item["PortName"]; row["PrinterStatus"] = item["PrinterStatus"]; results.Add(row); } string timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss"); string outPath = $"printer-snapshot_{timestamp}.json"; File.WriteAllText(outPath, JsonSerializer.Serialize(results, new JsonSerializerOptions { WriteIndented = true })); Console.WriteLine($"Snapshot of Printers ({results.Count} items) saved to {outPath}");