0

There was a local printer in =>Control Panel\All Control Panel Items\Devices and Printers. and I deleted it manually by clicking "remove device". and the printer did get removed, not showing up anymore.

the question is, we have .net code below to check if a printer installed or not. it returns true. it seems the printer still exist if we do 'SELECT * FROM Win32_Printer' by name.

Questions:

1) can someone give me more details, why the local printer record still exists?

2) can I trust the code below, or maybe I need more conditions to check the printer installation?

ManagementScope oManagementScope = new ManagementScope(ManagementPath.DefaultPath);
oManagementScope.Connect();
SelectQuery oSelectQuery = new SelectQuery();
oSelectQuery.QueryString = @"SELECT * FROM Win32_Printer WHERE Name = '" +  sPrinterName.Replace("\\", "\\\\") + "'";
using(ManagementObjectSearcher oObjectSearcher = new ManagementObjectSearcher(oManagementScope, oSelectQuery))
{
    using(ManagementObjectCollection oObjectCollection = oObjectSearcher.Get())
    {
        return oObjectCollection.Count > 0;
    }
}
Victor Xie
  • 148
  • 1
  • 9

2 Answers2

0

after some deep research, BTW, my windows is 8.1.

1) question still valid. (welcome any comments). My guess would be : when i removed the printer, some error happened, and the error blocks other printers showing up while windows populating the printer list.

2) The way I used to check printer installation is valid. There must be a bug/permission issue in printer driver or windows while removing the printer.

my solution for this situation (if you are trying to install a printer, it says the printer already exists and not showing up in printer list), you can try this:

1) open windows powershell, run Get-Printer, it list all printers, compare it with the printer list in control panel, remember which one is missing. Microsoft Get-Printer instruction

2) use the same powershell, run Remove-Printer by name, remove the printer not showing up. Microsoft Remove-Printer instruction

3) try to reinstall the printer.

in my case, after I reinstall the printer and then delete it, everything back to normal. I couldn't find any information can confirm the issue and the workaround. but it does exist. Hope it can help some body. Good Luck.

Victor Xie
  • 148
  • 1
  • 9
0

Win32_Printer is derived from CIM_Printer which is derived from CIM_LogicalDevice. So point being somehow there are remnants of printer information in any of these tables. If you want to make sure, check in below mentioned tables after running your deletion code:

  • CIM_LogicalDevice
  • Win32_PnpEntity

You can also use below mentioned query to delete the printer. I have not TESTED it yet.

$wshNetwork = New-Object -ComObject WScript.Network
$wshNetwork.RemovePrinterConnection("\\prntsvr01\myprinter")
Amit Shakya
  • 1,396
  • 12
  • 27
  • it is hard to reproduce the case. If I see the case again, I will try the tables you provided. Thanks a lot. – Victor Xie Jun 20 '16 at 13:10