Question

I want to set a local printer as default in C#

I have found two ways on Google search:

  1. Using IWshRuntimeLibrary and IWshNetwork_Class
  2. [DllImport("Winspool.drv")] private static extern bool SetDefaultPrinter(string printerName);

I found #1 in VB on http://social.msdn.microsoft.com/Forums/vstudio/en-US/1ed0f29a-a5ff-4328-9c0f-f431f75fccbc/set-defualt-printer?forum=vbgeneral

Its C# Code:

new IWshRuntimeLibrary.IWshNetwork_Class().SetDefaultPrinter(printerName)

So, #1 is a COM interop call. #2 is a straightforward dll call.

I have tried both and both work fine. So, which on should I use? Do I need to install a separate library (on client's machine) for #1 ?

Was it helpful?

Solution

I would choose the direct DLL call. I would bet that IWshRuntimeLibrary calls Winspool.drv internally, and you take the COM layer out of the picture. It seems unlikely that a windows machine would not have Windows Scripting Host registered properly, but it seems even less likely that Winspool.drv is not available.

OTHER TIPS

I think .NET has what you are looking for in the form of a managed solution. Try checking out the LocalPrintServer class and specifically the DefaultPrintQueue property. And here is some example code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top