using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class IISSettingDumper
{
public static void Dump()
{
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementScope scope = new ManagementScope(@"\\.\root\MicrosoftIISv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM IISWebVirtualDirSetting");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection res = searcher.Get();
JArray pool = new JArray();
foreach (var m in res)
{
JObject jo = new JObject();
foreach (var p in m.Properties)
{
if (p.Value is ManagementBaseObject[])
{
var jsonArray = new JArray();
var array = (ManagementBaseObject[])p.Value;
foreach (var q in array)
{
var child = new JObject();
foreach (var childProp in q.Properties)
child.Add(childProp.Name, new JValue(childProp.Value));
jsonArray.Add(child);
}
jo.Add(p.Name, jsonArray);
}
else if (p.Value != null && p.Value.GetType().IsArray)
{
var jsonArray = new JArray(p.Value);
jo.Add(p.Name, jsonArray);
}
else
{
jo.Add(p.Name, new JValue(p.Value));
}
}
pool.Add(jo);
}
System.IO.File.WriteAllText("c:\\temp\\VirDirSetting.json", pool.ToString());
Console.WriteLine(pool.ToString());
}
}