我编写代码来获取安装在我系统中的打印机名称。但我不知道要获得该打印机端口名称。这里是我写的代码。
public MainWindow()
{
this.InitializeComponent();
PrintServer server = new PrintServer();
foreach (PrintQueue queue in server.GetPrintQueues())
{
cboPrinters.Items.Add(queue.FullName);
}
var DefaultPrinter = new LocalPrintServer().DefaultPrintQueue;
string default_name=DefaultPrinter.FullName;
txxt_default.Text = "The default printe is "+" "+default_name;
}
它的结果将是这样的
我必须选择打印机的端口名称“DOP7”(如下图所示)。请帮助我。
答案 0 :(得分:2)
您是否尝试过PrintQueue.QueuePort Property?
string default_port = DefaultPrinter.QueuePort.Name;
<强>更新强>
然后我建议将PrintQueue
对象添加到ComboBox
public MainWindow()
{
InitializeComponent();
PrintServer server = new PrintServer();
foreach (PrintQueue queue in server.GetPrintQueues())
{
cboPrinters.Items.Add(queue);
}
}
并使用DisplayMemberPath
展示FullName
:
<ComboBox Name="cboPrinters"
DisplayMemberPath="FullName"
SelectionChanged="cboPrinters_SelectionChanged" />
在EventHandler中,您可以获得如下端口名称:
private void cboPrinters_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PrintQueue printer = cboPrinters.SelectedItem as PrintQueue;
string portname = printer.QueuePort.Name;
}