473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

get the default printer

Friends,

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

Thanks a lot
Pablo
Jul 21 '05 #1
1 12970
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:
//
http://msdn.microsoft.com/library/en...32_printer.asp
//
// 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.ObjectQuery(strQuery);

System.Management.ManagementObjectSearcher query1 = new
System.Management.ManagementObjectSearcher(oq);
System.Management.ManagementObjectCollection queryCollection1 =
query1.Get();
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}",
mo["Location"]);

// 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,
mo[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

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

//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: " +
outParams["returnValue"]);
}

}
}


"Pablo" <t-******@infocorp.com.uy> wrote in message
news:86****************************@phx.gbl...
Friends,

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

Thanks a lot
Pablo

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Esmee | last post by:
Hi there, I have created an Access 2002 db which contains several reports. Some reports need to be printed on a Laserjet and others on a Labelprinter. One of my clients wants to be able to print...
2
by: MLH | last post by:
I would like to allow users to set, as an application default, a specific printer to send reports to. I don't know how to bring up a list of valid print devices in A97, nor do I know how to...
2
by: Willem | last post by:
Hi, I'm trying to get the default printer using WMI. I use the following code: Private Function GetDefaultPrinter() As String Dim objWMIService As Object Dim colInstalledPrinters As Object...
1
by: Pablo | last post by:
Friends, how can i get the name of the default printer??. I'm working with winforms. VB,C#, nevermind. Thanks a lot Pablo
0
by: Karthick_Microsoft | last post by:
Actually, in my ASP.Net application, i have a requirement of automatically changing the default printer. I did a OCX using VB and put it on the web page. The client machines do not have local...
1
by: i8mypnuts | last post by:
Could someone please help? I am using the 'defaultprt.zip' tool provided by Ken Getz to change the default printer via VBA code (code below). My problem is that once the default printer has been...
1
by: groulder | last post by:
hi all, i have a problem with a network database that's used by a lot of people. people are finding, they will start the database, and if the mouse is moved over the print icon, the tooltip...
2
sashi
by: sashi | last post by:
Set default printer You will often find yourself in a position where you have designed a report format for one printer and when that report is sent to another printer the layout is messed up. ...
3
by: brianflannery | last post by:
Greetings all! My problem is this. I've installed a new printer on my computer. This is the "default printer" for access. Now, some of my reports, which were working fine with the old printer,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.