Solved

Default Printer

Posted on 2011-03-18
3
272 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 19

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now