By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,221 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,221 IT Pros & Developers. It's quick & easy.

get the default printer

P: n/a

how can i get the name of the default printer??. I'm
working with winforms. VB,C#, nevermind.

Thanks a lot
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
Pablo, You can use WMI to get and set the default printer.
Example follows.

Ionic Shade
Microsoft Developer Division
// Printer.cs
// sets the default printer using WMI
// references:
// Fri, 19 Sep 2003 14:14
public class PrinterTest {

public static void Main(string[] args) {

string PrinterToSelect= null;
if (args.Length>0) {
if (!(args[0].StartsWith("-")))
PrinterToSelect= args[0];
else {
System.Console.WriteLine("\nThis utility lists the configured
printers, and optionally selects\na new default printer.\n");
System.Console.WriteLine("usage: Printer [<name of printer to
select as default>]");
return ;
// Get the list of configured printers:
string strQuery= "SELECT * FROM Win32_Printer";

System.Console.WriteLine("WMI Query: '{0}'", strQuery);

System.Console.WriteLine("======================== ======================");
System.Management.ObjectQuery oq = new

System.Management.ManagementObjectSearcher query1 = new
System.Management.ManagementObjectCollection queryCollection1 =
System.Management.ManagementObject newDefault= null;

foreach( System.Management.ManagementObject mo in queryCollection1 ) {
System.Management.PropertyDataCollection pdc = mo.Properties;
System.Console.WriteLine("'{0}'", mo["Name"]);
if ((bool)mo["Local"])
System.Console.WriteLine(" A local printer");
else if ((bool)mo["Network"])
System.Console.WriteLine(" a network printer located at: {0}",

// if you want to display all properties of every printer
// foreach (System.Management.PropertyData pd in pdc) {
// System.Console.WriteLine(" {0:12} : {1}", pd.Name,
// }
if ((bool)mo["Default"]) {
System.Console.WriteLine(" THIS IS THE DEFAULT PRINTER");
else if (mo["Name"].ToString().Trim()==PrinterToSelect)
newDefault= mo; // store for later

if (newDefault!=null) {
System.Console.WriteLine("\nRESETTING THE DEFAULT PRINTER to '{0}'",

//Execute the method
System.Management.ManagementBaseObject outParams =
newDefault.InvokeMethod ("SetDefaultPrinter", null, null);

//Display results
//Note: The return code of the method is provided in the "returnValue"
property of the outParams object
System.Console.WriteLine("SetDefaultPrinter() returned: " +


"Pablo" <t-******> wrote in message

how can i get the name of the default printer??. I'm
working with winforms. VB,C#, nevermind.

Thanks a lot

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.