Click here to Skip to main content
Click here to Skip to main content

One small thing about printers and Windows

By , 16 Jun 2012
 

Last night I coded a project in C#. Its function is to list all printers that connect to a PC and make the default printer print. Print documents are images. I've never worked in that kind of work so I Googled and got some tuts. The tuts said: "Honey, just create a PrintDocument object, the freshly-created one contains a list and the first element is the default printer." I believe them, I did as they told without testing and delivered to my customer. Bullsh!t, it was not entirely not right.

I had to go back to the traditional way, access the system and read some info blah blah. You should know that I wrote a small e-book about WMI, surely I used this tool. The WMI code to get the name of the default printer in Windows OS is shown here:

public string GetDefaultPrinterName()
{
    var query = new ObjectQuery("SELECT * FROM Win32_Printer");
    var searcher = new ManagementObjectSearcher(query);

    foreach (ManagementObject mo in searcher.Get())
    {
        if (((bool?)mo["Default"]) ?? false)
        {
            return mo["Name"] as string;
        }
    }

    return null;
}

My mate Bình Minh said that, sometimes she read her old source code, still understand it but she doesn't really know why she could write these. I am writing this blog post to prevent this situation :)) Have fun.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duc Huy Nguyen
Vietnam Vietnam
Member
Oops!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 16 Jun 2012
Article Copyright 2012 by Duc Huy Nguyen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid