Remove printer and driver

Asked

Viewed 176 times

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 only the printer is removed, but the drive is not, if you do not remove the drive, using the Win32_printerdriver class you can remove?

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 don’t know exactly how to do it but it seems that there is an approximate solution to what you want in a question in the OS. It is in VBS but I think there is possible to see all the steps necessary to perform the task completely. Note that it is necessary to delete other system components.

Maybe the most relevant part is in the answer but see the whole so as not to do the service by half.

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

I put in the Github for future reference.

  • Thanks, already gave me a good way, I edited the question and put the code I’m using to remove, but is giving an error Generic Failure.

  • Editing is usually done when you go better at the same question by adding information that helps with that question. If you are going to ask another question you should not use the edit if not the answers given become invalid.

  • 1

    Okay, I’m back to the original question!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.