How to change the wallpaper on windows with C#

The following code can be used as follows:

var screen = Screen.PrimaryScreen;
var bounds = screen.Bounds;
var workingArea = screen.WorkingArea;

var bitmap = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(bitmap))
{
    g.Clear(Color.White);
    // Draw stuff... or scrap the graphics and load from a file or stream!
}
Wallpaper.Set(bitmap, Wallpaper.Style.Centered);

Continue reading “How to change the wallpaper on windows with C#”

Seakable Stream

If you’ve been programming anything that’s even a little bit interesting, you know streams. There are streams that can write, there are streams that can read, there are streams that can do both, there are streams that can seek, and there are streams that cannot. Here’s where SeakableStream comes in: if you have a stream that cannot seek and you need to be able to go back and have another look, then you can use my SeakableStream class.
Continue reading “Seakable Stream”

Calling a method on a management object

Here’s an example of calling a method on a management object. Specifically, this code show how to call the method GetDeviceProperties on an instance of the Win32_PnpEntity class.

using System;
using System.Linq;
using System.Management;
...
using (var searcher = new ManagementObjectSearcher("Select * From Win32_PnPEntity WHERE Caption Like \"%Shapeoko%\""))
{
    foreach (var item in searcher.Get().Cast<ManagementObject>())
    {
        item.Dump();
        var params1 = new object[] { null, new object[] { } };
        var result0 = item.InvokeMethod("GetDeviceProperties", params1);

        var props = (ManagementBaseObject[])params1[1];
        foreach (var item1 in props)
        {
            item1.Dump();
        }
        var port = item.GetRelated("Win32_SerialPort").Cast<ManagementObject>().First();
        port.Dump();
    }
}

The method Dump, as you may know, is not actually implemented by the ManagementObject class – it is an extension method that I have implemented. See my previous post on this if you are interested.

Running this code produces the following output:
Continue reading “Calling a method on a management object”

Demystifying ManagementObject instances

If you’ve ever worked with the Windows Management Instrumentation (WMI) infrastructure, you know how annoying it is that you can’t really watch instances of the System.Management.ManagementObject class or, its parent, the System.Management.ManagementBaseObject class – either the properties time out, or you have to click so far down in the watch window that you get lost. What’s worse, most objects are not properly documented.

To make a bit easier to see what’s in there, I’ve written an extension method for the ManagementBaseObject class which you can find below the fold.
Continue reading “Demystifying ManagementObject instances”