Solved

Default Printer

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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