在Windows Vista上使用C#打印测试页

在Windows Vista上使用C#打印测试页,c#,windows-vista,printing,C#,Windows Vista,Printing,在网络上安装打印机后打印测试页时,我遇到了一个非常奇怪的问题。这一切都非常好,当以管理员身份运行代码时。。。测试页也会打印出来。但当我“正常”运行程序时,我得到一个“访问被拒绝” 下面是代码: // get the printer string path = "Win32_Printer.DeviceId='" + printerName + "'"; using (ManagementObject printer = new ManagementObject(path)) { // i

在网络上安装打印机后打印测试页时,我遇到了一个非常奇怪的问题。这一切都非常好,当以管理员身份运行代码时。。。测试页也会打印出来。但当我“正常”运行程序时,我得到一个“访问被拒绝”

下面是代码:

// 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始终为5,表示“访问被拒绝”

当我以同样的方式调用“SetDefaultPrinter”方法时,无论是作为管理员还是非管理员,一切都很好

希望每个人都有线索

谢谢,
西蒙解决了这个问题。我找到了另一段代码,它做同样的事情,并且在两种上下文中都能工作

WshShell shell=新的WshShell(); string command=“RUNDLL32 PRINTUI.DLL,PRINTUI条目/k/n\”“+printerInfo.PrinterName+”\”; 对象windowStyle=null; 对象waitOnReturn=null; Run(命令,ref windowStyle,ref waitOnReturn)

就这样。 无论如何,谢谢你

干杯,
西蒙解决了这个问题。我找到了另一段代码,它做同样的事情,并且在两种上下文中都能工作

WshShell shell=新的WshShell(); string command=“RUNDLL32 PRINTUI.DLL,PRINTUI条目/k/n\”“+printerInfo.PrinterName+”\”; 对象windowStyle=null; 对象waitOnReturn=null; Run(命令,ref windowStyle,ref waitOnReturn)

就这样。 无论如何,谢谢你

干杯, 西蒙