C# 如何获取计算机中所有打印机的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2354435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the list of all printers in computer
提问by Gold
I need to get the list of all printers that connect to computer?
我需要获取连接到计算机的所有打印机的列表吗?
How I can do it in C#, WinForms?
我如何在 C#、WinForms 中做到这一点?
采纳答案by Jojo Sardez
Try this:
尝试这个:
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
MessageBox.Show(printer);
}
回答by Rune Grimstad
Look at the static System.Drawing.Printing.PrinterSettings.InstalledPrintersproperty.
查看静态System.Drawing.Printing.PrinterSettings.InstalledPrinters属性。
It is a list of the names of all installed printers on the system.
它是系统上所有已安装打印机的名称列表。
回答by Christian Moser
If you need more information than just the name of the printer you can use the System.Management
API to query them:
如果您需要更多信息而不仅仅是打印机名称,您可以使用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);
}
回答by Code Scratcher
Get Network and Local Printer List in ASP.NET
在 ASP.NET 中获取网络和本地打印机列表
This method uses the Windows Management Instrumentation or the WMI interface. It's a technology used to get information about various systems (hardware) running on a Windows Operating System.
此方法使用 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());
}
}
Click here to download source and application demo
Demo of application which listed network and local printer
列出网络和本地打印机的应用程序演示
回答by Hernan Alonso
You can also use the LocalPrintServer class. See: System.Printing.LocalPrintServer
您还可以使用 LocalPrintServer 类。请参阅:System.Printing.LocalPrintServer
public List<string> InstalledPrinters
{
get
{
return (from PrintQueue printer in new LocalPrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
EnumeratedPrintQueueTypes.Connections }).ToList()
select printer.Name).ToList();
}
}
As stated in the docs: Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service.
如文档中所述:不支持在 Windows 服务或 ASP.NET 应用程序或服务中使用 System.Printing 命名空间中的类。
回答by RUBEN
If you are working with MVC C#, this is the way to deal with printers and serial ports for dropdowns.
如果您正在使用 MVC C#,这是处理下拉列表的打印机和串行端口的方法。
using System.Collections.Generic;
using System.Linq;
using System.IO.Ports;
using System.Drawing.Printing;
public class Miclass
{
private void AllViews()
{
List<PortClass> ports = new List<PortClass>();
List<Printersclass> Printersfor = new List<Printersclass>();
string[] portnames = SerialPort.GetPortNames();
/*PORTS*/
for (int i = 0; i < portnames.Count(); i++)
{
ports.Add(new PortClass() { Name = portnames[i].Trim(), Desc = portnames[i].Trim() });
}
/*PRINTER*/
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
Printersfor.Add(new Printersclass() { Name = PrinterSettings.InstalledPrinters[i].Trim(), Desc = PrinterSettings.InstalledPrinters[i].Trim() });
}
}
}
public class PortClass
{
public string Name { get; set; }
public string Desc { get; set; }
public override string ToString()
{
return string.Format("{0} ({1})", Name, Desc);
}
}
public class Printersclass
{
public string Name { get; set; }
public string Desc { get; set; }
public override string ToString()
{
return string.Format("{0} ({1})", Name, Desc);
}
}