[點晴永久免費OA]C#獲取打印機工作狀態(tài),是否離線,打印機名稱,端口號等打印機屬性信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class FrmGetPrinterStatus : Form
{
public FrmGetPrinterStatus()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string query = string.Format("select * from Win32_Printer ");
var searcher = new ManagementObjectSearcher(query);
var printers = searcher.Get();
foreach (var printer in printers)
{
//顯示部分屬性
Console.WriteLine(printer.Properties["Name"].Value); //獲取打印機名稱
Console.WriteLine(printer.Properties["PortName"].Value); //獲取打印機端口號
Console.WriteLine(printer.Properties["Default"].Value); //獲取打印機是否是默認打印機
Console.WriteLine(printer.Properties["PrinterStatus"].Value); //獲取打印機工作狀態(tài)
Console.WriteLine(printer.Properties["WorkOffline"].Value); //獲取打印機是否離線
//顯示所有屬性到即時窗口
foreach (var property in printer.Properties)
{
Console.WriteLine(string.Format("\t{0}: {1}", property.Name, property.Value));
}
Console.WriteLine();
}
}
}
}
該文章在 2022/7/20 11:10:20 編輯過 |
關鍵字查詢
相關文章
正在查詢... |