How to remove printer from .Net print dialog?

I am working on a Winforms application that allows users to print multiple Reporting Services reports. Unfortunately, if a user tries to print to PDF using an Adobe PDF printer, it will work. We could not solve this problem, since the work around we want to remove the ability for users to print to an Adobe PDF printer.

Is there a way to programmatically remove an Adobe PDF printer from the list of printers in the print dialog?

+3
source share
2 answers

Call this with the printer name before calling PrintDialog () .... I think this will solve your problem

public bool RemovePrinter(string printerName)
{
        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(printerName.ToLower()))
            {
                printer.Delete();
                break;
            }
        }                    

        return true;
}
+2
source

manish , . , , , Printer (1), Printer (2) .. , WMI .

using System.Management;
//...
var scope = new ManagementScope(ManagementPath.DefaultPath);
scope.Connect();
var query = new SelectQuery($@"select * from Win32_Printer where Name like '{PrinterDeviceName}%'");
foreach (var o in new ManagementObjectSearcher(scope, query).Get()) 
    ((ManagementObject) o).Delete();

System.Management.

0

Source: https://habr.com/ru/post/1776799/


All Articles