Solved

Default Printer

Posted on 2011-03-18
3
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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