473,625 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13010
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("\nTh is utility lists the configured
printers, and optionally selects\na new default printer.\n");
System.Console. WriteLine("usag e: 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.Manageme nt.ObjectQuery oq = new
System.Manageme nt.ObjectQuery( strQuery);

System.Manageme nt.ManagementOb jectSearcher query1 = new
System.Manageme nt.ManagementOb jectSearcher(oq );
System.Manageme nt.ManagementOb jectCollection queryCollection 1 =
query1.Get();
System.Manageme nt.ManagementOb ject newDefault= null;

foreach( System.Manageme nt.ManagementOb ject mo in queryCollection 1 ) {
System.Manageme nt.PropertyData Collection 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.Managem ent.PropertyDat a 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().Tri m()==PrinterToS elect)
newDefault= mo; // store for later

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

//Execute the method
System.Manageme nt.ManagementBa seObject outParams =
newDefault.Invo keMethod ("SetDefaultPri nter", null, null);

//Display results
//Note: The return code of the method is provided in the "returnValu e"
property of the outParams object
System.Console. WriteLine("SetD efaultPrinter() returned: " +
outParams["returnValu e"]);
}

}
}


"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
2574
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 the same reports to 3 different laserjets and to 3 different labelprinters. (They work with the db on 3 different units each with their own Laserjet and Labelprinter) For each unit there is a copy of all the reports. The client can make a unit...
2
2101
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 reference any one of them that might be selected. For instance, if I click Start, Control Panel, Printers and Faxes on a PC running XP and I see the following: HP Laser Jet 4200 PCL 6 HP Laser Jet 4200 PCL 5e HP Laser Jet III
2
5583
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 Dim objPrinter As Object
1
463
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
1608
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 printers but network printers (they have been mapped using Local Port pointing to \\machine\printer - this was suggested in a topic - for a reason that a network printer can be given a friendly name) - But when I click on Print from the browser...
1
5448
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 changed, Access 2003 still directs the report to the former default printer. Access 2003 picks up the new default printer setting only after I have reopened the application. I have checked the default printer setting in the OS control panel and the...
1
2357
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 appears to show which printer it will print to as normal. this printer however is a printer that the user doesn't even have installed.
2
17360
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. The following code will allow you to specify the printer that reports are sent to by reseting the systems default printer. Windows API/Global Declarations Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA"...
3
4273
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, are now showing up with an odd view (very enlarged) in preview. If I go to page setup, and choose specific printer to then look at the paper size listed in my default printer, it shows Hegaki Card 100mmX148mm instead of letter. It seems as...
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.