本文小编为大家详细介绍“C#如何获取打印机列表”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#如何获取打印机列表”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
第一步:添加引用
using System.Printing;
第二步:代码
public static List<string> GetPrintList() { List<string> lt = new List<string>(); LocalPrintServer printServer = new LocalPrintServer(); PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local }); foreach (PrintQueue printer in printQueuesOnLocalServer) lt.Add(printer.Name); return lt; }
需要引入
System.Drawing.Printing System.Management
1.获取打印机列表
2.获取默认打印机
3.设置默认打印机
public class PrinterExample { /// <summary> /// 设置默认打印机 /// </summary> /// <param name="Name"></param> /// <returns></returns> [DllImport("winspool.drv")] public static extern bool SetDefaultPrinter(String Name); //调用win api将指定名称的打印机设置为默认打印机 /// <summary> /// 获取默认打印机 /// </summary> /// <returns></returns> public static string GetDefaultPrinter() { PrintDocument print = new PrintDocument(); string defaultName = print.PrinterSettings.PrinterName;//默认打印机名 return defaultName; } /// <summary> /// 获取本地所有打印机 /// </summary> /// <returns></returns> public static List<String> GetLocalPrinters() { List<String> printer_names = new List<String>(); foreach (String item in PrinterSettings.InstalledPrinters) { printer_names.Add(item); } return printer_names; } /// <summary> /// 获取本地所有打印机 System.Management方式获取 /// </summary> /// <returns></returns> public static List<String> GetLocalPrinters2() { List<String> printer_names = new List<String>(); ManagementObjectCollection queryCollection; string _classname = "SELECT * FROM Win32_Printer"; Dictionary<string, ManagementObject> dict = new Dictionary<string, ManagementObject>(); ManagementObjectSearcher query = new ManagementObjectSearcher(_classname); queryCollection = query.Get(); foreach (ManagementObject mo in queryCollection) { string oldName = mo["Name"].ToString(); printer_names.Add(oldName); } return printer_names; } }
读到这里,这篇“C#如何获取打印机列表”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。