我正在尝试从Windows服务更改打印机的驱动程序。我使用以下代码
ManagementObjectSearcher printers = new ManagementObjectSearcher("Select * from Win32_Printer");
foreach (System.Management.ManagementObject printer in printers.Get())
{
string printerName = (string)printer.GetPropertyValue("name");
if (printerName.ToLower().Contains("canon") && printerName.ToLower().Contains("ip110"))
{
imprimanteCanonPresente = true;
bool genericDriverInstalled = GenericDriverInstalled();
if (genericDriverInstalled)
{
string driverName = (string)printer.GetPropertyValue("DriverName");
if (!string.Equals(driverName, CANON_GENERIC_DRIVER_NAME))
{
printer.SetPropertyValue("DriverName", CANON_GENERIC_DRIVER_NAME);
printer.Put(); // error, access denied
}
}
}
}
这适用于visual studio,但不适用于服务(“拒绝访问”)。该服务由“本地服务”开放。
是否有解决问题的方法或者可能是另一种更改驱动程序的解决方案? 感谢