Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Default Printer

Posted on 2011-03-18
3
Medium Priority
?
328 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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