您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。