Solved

Default Printer

Posted on 2011-03-18
3
289 Views
Last Modified: 2012-05-11
I am using the following code to get list of printers and then to see whether it is default printer.

public static bool PaperSize()
        {
            foreach (string printerName in PrinterSettings.InstalledPrinters)
            {
                var printer = new PrinterSettings();
                printer.PrinterName = printerName;


                if (printer.IsDefaultPrinter)
                {
                    foreach (PaperSize size in printer.PaperSizes)
                    {
                        if (Enum.IsDefined(size.Kind.GetType(), size.Kind))
                        {
                            string PSize = size.Kind.ToString();
                            if (PSize == "A4")
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

Problem
If the default printer is network printer, it returns false ! How to solve this
0
Comment
Question by:GRChandrashekar
3 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35165443
That does more than see if it's the default printer.  It checks if it's the default and supports A4 paper size.  Maybe your network printer(s) don't accept A4.
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 35165610
Okay.  Here is your method.  Note the information about paper sizes is found here:
http://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

You'll need to add System.Management to your references and then using System.Management to your namespace list:

It *should* work with all printer types (networked and local), but I've not tested it with local as I have no local printers on my system.
public static bool PaperSize()
        {
            bool isPaperSize = false;
            const string query = "SELECT * FROM Win32_Printer";
            bool isDefault = false;

            ObjectQuery objectQuery = new ObjectQuery( query );

            ManagementObjectSearcher searcher = new ManagementObjectSearcher( objectQuery );

            ManagementObjectCollection collection = searcher.Get();

            foreach ( ManagementObject mo in collection )
            {
                PropertyDataCollection properties = mo.Properties;

                foreach ( PropertyData pd in properties )
                {
                    if ( pd != null && string.Equals( pd.Name, "Default", StringComparison.InvariantCultureIgnoreCase ) )
                    {
                        if ( pd.Value != null )
                        {
                            bool.TryParse( pd.Value.ToString(), out isDefault );
                        }
                    }

                    if ( pd != null && !string.IsNullOrEmpty( pd.Name ) && isDefault )
                    {
                        if ( string.Equals( pd.Name, "PaperSizesSupported", StringComparison.InvariantCultureIgnoreCase ) )
                        {
                            if ( pd.Value != null )
                            {
                                UInt16[] sizes = (UInt16[])pd.Value;
                                foreach ( UInt16 size in sizes )
                                {
                                    if ( size == 22 )
                                    {
                                        isPaperSize = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return isPaperSize;
        }

Open in new window

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35167845
Hello,

public static string DefaultPrinterName()
{
string functionReturnValue = null;
System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings(…

try {
functionReturnValue = oPS.PrinterName;
}
catch (System.Exception ex) {
functionReturnValue = "";
}
finally {
oPS = null;
}
return functionReturnValue;
}
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question