[C#][VB.NET]取得印表機資訊

  • 6199
  • 0
  • C#
  • 2011-06-28

摘要:C# 取得印表機資訊

在程式俱樂部回答網友問題時 找到的資料順手記錄下來

Changing printer settings using C#

 

若是要取得印表機清單 可參考

[C#] [VB.NET]取得安裝於電腦上的所有印表機名稱與預設印表機名稱

或是利用 WMI

C#

string strDefaultPrinterName="";
            ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * From Win32_Printer");
            foreach (ManagementObject mo in mos.Get())
            {
                comboBox1.Items.Add(mo["DeviceID"]);
                if (mo.Properties["Default"].Value != null)
                {
                    if (Convert.ToBoolean(mo.Properties["Default"].Value)) 
                    strDefaultPrinterName = mo["DeviceID"].ToString();
                }
            }
            comboBox1.Sorted = true;
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf(strDefaultPrinterName);

 

VB.Net

Dim strDefaultPrinterName As String = ""
        Dim mos As New ManagementObjectSearcher("Select * From Win32_Printer")
        For Each mo As ManagementObject In mos.Get()

            ComboBox1.Items.Add(mo("DeviceID"))
            If (mo.Properties("Default").Value IsNot Nothing) Then
                If (Convert.ToBoolean(mo.Properties("Default").Value)) Then
                    strDefaultPrinterName = mo("DeviceID").ToString()
                End If
            End If
        Next
        ComboBox1.Sorted = True
        ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf(strDefaultPrinterName)

 

 

更多的參考資料

MSDN Printing and Print Spooler Functions