如何获取计算机中所有打印机的列表
我需要获取连接到计算机的所有打印机的列表?
我怎样才能在C#,WinForms中做到这一点?
尝试这个:
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { MessageBox.Show(printer); }
如果您需要更多信息而不仅仅是打印机的名称,则可以使用System.Management
API来查询它们:
var printerQuery = new ManagementObjectSearcher("SELECT * from Win32_Printer"); foreach (var printer in printerQuery.Get()) { var name = printer.GetPropertyValue("Name"); var status = printer.GetPropertyValue("Status"); var isDefault = printer.GetPropertyValue("Default"); var isNetworkPrinter = printer.GetPropertyValue("Network"); Console.WriteLine("{0} (Status: {1}, Default: {2}, Network: {3}", name, status, isDefault, isNetworkPrinter); }
取自以下网页的示例: http : //www.snippetsource.net/Snippet/96/list-all-installed-printers
看看静态的System.Drawing.Printing.PrinterSettings.InstalledPrinters属性。
它是系统上所有已安装打印机的名称列表。
在ASP.NET中获取networking和本地打印机列表
此方法使用Windows Management Instrumentation或WMI界面。 这是一种用于获取有关在Windows操作系统上运行的各种系统(硬件)信息的技术。
private void GetAllPrinterList() { ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access objScope.Connect(); SelectQuery selectQuery = new SelectQuery(); selectQuery.QueryString = "Select * from win32_Printer"; ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery); ManagementObjectCollection MOC = MOS.Get(); foreach (ManagementObject mo in MOC) { lstPrinterList.Items.Add(mo["Name"].ToString()); } }
点击这里下载源代码和应用程序演示
列出networking和本地打印机的应用程序的演示
Imports System.Drawing.Printing Imports System.Reflection Imports System.Runtime.InteropServices Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim pkInstalledPrinters As String ' Find all printers installed For Each pkInstalledPrinters In _ PrinterSettings.InstalledPrinters printList.Items.Add(pkInstalledPrinters) Next pkInstalledPrinters ' Set the combo to the first printer in the list If printList.Items.Count > 0 Then printList.SelectedItem = 0 End If End Sub End Class