Click here to Skip to main content
15,886,689 members
Articles / Operating Systems / Windows

One Small Thing About Printers and Windows

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Jun 2012CPOL 8.4K   3  
One small thing about printers and windows

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 did Googling and got some tuts.

The tuts said: "Honey, just create a PrintDocument object, the fresh-created one contains a list and first element is the default printer."

I believe them, I did as they told without testing and delivered to my customer.

Bullsh!t, it is ENTIRELY not true.

I had to be 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 default printer in Windows OS is here:

C#
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 reads her old source code, still understands 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)


Written By
Vietnam Vietnam
Oops!

Comments and Discussions

 
-- There are no messages in this forum --