1

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 ?

4

2 回答 2

2

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.

于 2013-10-15T22:16:12.140 回答
1

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

于 2013-10-15T22:34:22.393 回答