🟣 C# / .NET 6+

Backup CD/DVD Drives to JSON (C#)

Snapshots CD/DVD Drives (Win32_CDROMDrive) to a timestamped JSON file via System.Management and System.Text.Json.

By WindowsScripting.com · 1.1 KB
BackupCDROMDriveListToJson.cs
// BackupCDROMDriveListToJson.cs
//
// Snapshots CD/DVD Drives (Win32_CDROMDrive) to a timestamped JSON file.
//
// Requires the System.Management package:
//   dotnet new console -o BackupCDROMDrive
//   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, Drive, MediaType FROM Win32_CDROMDrive");
var results = new List<Dictionary<string, object?>>();

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

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

Console.WriteLine($"Snapshot of CD/DVD Drives ({results.Count} items) saved to {outPath}");