views:

138

answers:

1

Hi all. I have a very odd problem when printing a test page after installing the printer on the network. That all works very good and when running the code As Administrator... the test page is also printed. But when I run the program "normally" I get an "Access is denied".

So here is the code:

// get the printer
string path = "Win32_Printer.DeviceId='" + printerName + "'";
using (ManagementObject printer = new ManagementObject(path))
{
    // invoke the method
    object obj = printer.InvokeMethod("PrintTestPage", null);
    System.Windows.Forms.MessageBox.Show(obj.ToString());
}

obj is always 5, meaning "Access is denied".

When I invoke the "SetDefaultPrinter" method the same way all is fine for both as administrator and not.

Hopefully anyone has a clue.

Thanks, Simon

+1  A: 

Hi all, Solved it. I found another piece of code that does the same and works in both context.

WshShell shell = new WshShell(); string command = "RUNDLL32 PRINTUI.DLL,PrintUIEntry /k /n \"" + printerInfo.PrinterName + "\""; object windowStyle = null; object waitOnReturn = null; shell.Run(command, ref windowStyle, ref waitOnReturn);

So that's it. Thanks anyway.

Cheers, Simon