🟣 C# / .NET 6+

Backup Keyboard Info to JSON (C#)

Snapshots Keyboard Info (Win32_Keyboard) to a timestamped JSON file via System.Management and System.Text.Json.

By WindowsScripting.com · 1.2 KB
BackupKeyboardListToJson.cs
// BackupKeyboardListToJson.cs
//
// Snapshots Keyboard Info (Win32_Keyboard) to a timestamped JSON file.
//
// Requires the System.Management package:
//   dotnet new console -o BackupKeyboard
//   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 Description, Layout, NumberOfFunctionKeys FROM Win32_Keyboard");
var results = new List<Dictionary<string, object?>>();

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

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

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