Remove printer and driver

1

I have the following code:

private void btnDeletar_Click(object sender, RoutedEventArgs e)
{
    string nomeImpressora = null;
    if(cmbImpressoras.SelectedIndex != -1)
        nomeImpressora = cmbImpressoras.SelectedItem.ToString();
    else
        MessageBox.Show("Selecione uma Impressora");

    ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
    scope.Connect();
    SelectQuery query = new SelectQuery("select * from Win32_Printer");
    ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection printers = search.Get();

    foreach (ManagementObject printer in printers)
    {
        string printerName = printer["Name"].ToString().ToLower();

        if (printerName.Equals(nomeImpressora.ToLower()))
        {
            try
            {
                DriverImpressora(printer["DriverName"].ToString());
                printer.Delete();

            }
            catch (Exception)
            {
                MessageBox.Show("Impossível remover impressora!");
            }
            break;
        }
    }
}

Now the doubt:

I think the printer is only removed, but the drive does not, if it does not remove the drive, using the Win32_PrinterDriver class is it possible to remove?

    
asked by anonymous 14.11.2014 / 13:32

1 answer

2

You already know the answer to the first question.

The second is possible, you're on the right track, but it's not that simple. I'm not exactly sure how to do it but it looks like there's a rough solution to what you want in a question in the SO . It's in VBS but I think it's possible to see all the steps needed to complete the task completely. Note that you need to delete other system components.

Perhaps the most relevant part is in the response but see the whole not to do the service in half.

qry = "SELECT * FROM Win32_PrinterDriver"
For Each driver In objWMIService.ExecQuery(qry)
  If driver.Name = "..." Then driver.Delete_
Next
    
14.11.2014 / 13:48