删除本地打印机

时间:2012-10-05 11:34:32

标签: vb.net printing

我有一个桌面应用程序,其listview在我的本地计算机上安装了已安装的打印机。这是一个简单的打印机管理,所以我希望能够右键单击一个项目,然后删除该特定的打印机。我怎么能这样做?

以下是我列出打印机的方法

Public Sub listInstalledPrinters()
        Dim i As Integer
        Dim pkInstalledPrinters As String

        For i = 0 To printersettings.InstalledPrinters.Count - 1
            pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)
            listViewLocal.Items.Add(pkInstalledPrinters, nIndex)
        Next

    End Sub

但是如何删除其中一个呢?是否有内置的方法来做到这一点?

1 个答案:

答案 0 :(得分:3)

如果您通过System.Management.ManagementScope查询打印机,则可以使用Delete类的ManagementObject方法轻松删除它们。

Dim scope = New ManagementScope(ManagementPath.DefaultPath)
scope.Connect()
Dim printers = New ManagementObjectSearcher(scope, new SelectQuery("select * from Win32_Printer")).[Get]()
For Each printer As ManagementObject In printers
    Dim nameOfPrinter = printer("Name").ToString()

     If nameOfPrinter = "ThePrinterYouWantToDelete" Then
         printer.Delete()
     End If
Next