🟣 C# / .NET 6+

Backup Network Adapter Configuration to JSON (C#)

Snapshots Network Adapter Configuration (Win32_NetworkAdapterConfiguration) to a timestamped JSON file via System.Management and System.Text.Json.

By WindowsScripting.com · 1.4 KB
BackupNetworkAdapterConfigListToJson.cs
// BackupNetworkAdapterConfigListToJson.cs
//
// Snapshots Network Adapter Configuration (Win32_NetworkAdapterConfiguration) to a timestamped JSON file.
//
// Requires the System.Management package:
//   dotnet new console -o BackupNetworkAdapterConfig
//   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, IPAddress, DefaultIPGateway, DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration");
var results = new List<Dictionary<string, object?>>();

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

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

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