473,786 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set Default Printer

How do you set the default printer in C#? I have added a printer and now
want to set that printer to the default. Listed below is my code, but I am
getting an "Invalid method Parameter(s)" error on the 4th line:

ManagementClass printerClass = new ManagementClass ("Win32_Printer ");
object[] printerName = {"\\\\REGWPROD\ \REGW0951"};
object result = printerClass.In vokeMethod("Add PrinterConnecti on",
printerName);
result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);

Can anyone tell me what I am missing?

Thanks
Andy
Nov 17 '05 #1
2 11349
I use this code in VB.Net. You shouldn't too much problem converting it:

Dim strOldPrinter As String

Dim WshNetwork As Object

Dim pd As New PrintDocument

Try

strOldPrinter = pd.PrinterSetti ngs.PrinterName

WshNetwork = Microsoft.Visua lBasic.CreateOb ject("WScript.N etwork")

WshNetwork.SetD efaultPrinter(s trPrinterName)

pd.PrinterSetti ngs.PrinterName = strPrinterName

If pd.PrinterSetti ngs.IsValid Then

Return True

Else

WshNetwork.SetD efaultPrinter(s trOldPrinter)

Return False

End If

Catch exptd As Exception

WshNetwork.SetD efaultPrinter(s trOldPrinter)

Return False

Finally

WshNetwork = Nothing

pd = Nothing

End Try
--
HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
(http://aspnet2.com/mvp.ashx?EricMoreau)
Conseiller Principal / Senior Consultant
Concept S2i inc. (www.s2i.com)

"Andy" <An**@discussio ns.microsoft.co m> wrote in message
news:C0******** *************** ***********@mic rosoft.com...
How do you set the default printer in C#? I have added a printer and now
want to set that printer to the default. Listed below is my code, but I
am
getting an "Invalid method Parameter(s)" error on the 4th line:

ManagementClass printerClass = new ManagementClass ("Win32_Printer ");
object[] printerName = {"\\\\REGWPROD\ \REGW0951"};
object result = printerClass.In vokeMethod("Add PrinterConnecti on",
printerName);
result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);

Can anyone tell me what I am missing?

Thanks
Andy

Nov 17 '05 #2
Eric:

Thank you

"Eric Moreau" wrote:
I use this code in VB.Net. You shouldn't too much problem converting it:

Dim strOldPrinter As String

Dim WshNetwork As Object

Dim pd As New PrintDocument

Try

strOldPrinter = pd.PrinterSetti ngs.PrinterName

WshNetwork = Microsoft.Visua lBasic.CreateOb ject("WScript.N etwork")

WshNetwork.SetD efaultPrinter(s trPrinterName)

pd.PrinterSetti ngs.PrinterName = strPrinterName

If pd.PrinterSetti ngs.IsValid Then

Return True

Else

WshNetwork.SetD efaultPrinter(s trOldPrinter)

Return False

End If

Catch exptd As Exception

WshNetwork.SetD efaultPrinter(s trOldPrinter)

Return False

Finally

WshNetwork = Nothing

pd = Nothing

End Try
--
HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
(http://aspnet2.com/mvp.ashx?EricMoreau)
Conseiller Principal / Senior Consultant
Concept S2i inc. (www.s2i.com)

"Andy" <An**@discussio ns.microsoft.co m> wrote in message
news:C0******** *************** ***********@mic rosoft.com...
How do you set the default printer in C#? I have added a printer and now
want to set that printer to the default. Listed below is my code, but I
am
getting an "Invalid method Parameter(s)" error on the 4th line:

ManagementClass printerClass = new ManagementClass ("Win32_Printer ");
object[] printerName = {"\\\\REGWPROD\ \REGW0951"};
object result = printerClass.In vokeMethod("Add PrinterConnecti on",
printerName);
result = printerClass.In vokeMethod("Set DefaultPrinter" , printerName);

Can anyone tell me what I am missing?

Thanks
Andy


Nov 17 '05 #3

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

Similar topics

1
13026
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
2592
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
2115
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
5587
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
3
25283
by: Raymond Martin | last post by:
How do I find out if the default printer is offline or online (or has a paper jam)?
0
1616
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
5469
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
2366
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
17450
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
4288
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
9655
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
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
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
8993
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
5398
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...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.