How to

enumerate desktops using WMI

Published: 12. February 2012 | Updated: 12. February 2012
License: Microsoft Public License (MS-PL)
Categories: Windows
Tags: C# Windows WMI
Was this snippet helpful for you? YESYES / NONO

Import namespaces

using System;
using System.Collections.Generic;
using System.Management; // requires adding System.Management reference to project

Code

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Desktop");
ManagementObjectCollection collection = searcher.Get();

var items = new List<Win32_Desktop>();
foreach (ManagementObject obj in collection)
{
    var item = new Win32_Desktop();
    item.BorderWidth = (uint?)obj["BorderWidth"];
    item.Caption = (string)obj["Caption"];
    item.CoolSwitch = (bool?)obj["CoolSwitch"];
    item.CursorBlinkRate = (uint?)obj["CursorBlinkRate"];
    item.Description = (string)obj["Description"];
    item.DragFullWindows = (bool?)obj["DragFullWindows"];
    item.GridGranularity = (uint?)obj["GridGranularity"];
    item.IconSpacing = (uint?)obj["IconSpacing"];
    item.IconTitleFaceName = (string)obj["IconTitleFaceName"];
    item.IconTitleSize = (uint?)obj["IconTitleSize"];
    item.IconTitleWrap = (bool?)obj["IconTitleWrap"];
    item.Name = (string)obj["Name"];
    item.Pattern = (string)obj["Pattern"];
    item.ScreenSaverActive = (bool?)obj["ScreenSaverActive"];
    item.ScreenSaverExecutable = (string)obj["ScreenSaverExecutable"];
    item.ScreenSaverSecure = (bool?)obj["ScreenSaverSecure"];
    item.ScreenSaverTimeout = (uint?)obj["ScreenSaverTimeout"];
    item.SettingID = (string)obj["SettingID"];
    item.Wallpaper = (string)obj["Wallpaper"];
    item.WallpaperStretched = (bool?)obj["WallpaperStretched"];
    item.WallpaperTiled = (bool?)obj["WallpaperTiled"];

    items.Add(item);
}

Class

public class Win32_Desktop
{
    public uint? BorderWidth;
    public string Caption;
    public bool? CoolSwitch;
    public uint? CursorBlinkRate;
    public string Description;
    public bool? DragFullWindows;
    public uint? GridGranularity;
    public uint? IconSpacing;
    public string IconTitleFaceName;
    public uint? IconTitleSize;
    public bool? IconTitleWrap;
    public string Name;
    public string Pattern;
    public bool? ScreenSaverActive;
    public string ScreenSaverExecutable;
    public bool? ScreenSaverSecure;
    public uint? ScreenSaverTimeout;
    public string SettingID;
    public string Wallpaper;
    public bool? WallpaperStretched;
    public bool? WallpaperTiled;
}
Send us feedback about this snippet »



Related Snippets: