使用 C# 查看本地已连接的打印机

C# 2020-09-13 阅读 115 评论 0

使用 C# 查询本地连接的所有打印机。下面介绍了2种方法。

1 使用 EnumeratedPrintQueueTypes

using System;
using System.Linq;
using System.Printing;

namespace PrinterApp
{
    class Printer
    {
        public void FindAllPrinter() {
            var server = new PrintServer();
            PrintQueueCollection queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
            var arr = queues.Select(q => q.QueuePort.Name + "  " + q.FullName).ToArray();
            Console.WriteLine(String.Join("\n", arr));
        }
    }
}

将打印:

Kingsoft Virtual Printer Port => 导出为WPS PDF
USB001 => Gprinter GP-1324D
USB001 => \\USER-20170818xb\Canon MF4400 Series UFRII LT

参考 EnumeratedPrintQueueTypes 的源码。可以指定多种类型。

using System;

namespace System.Printing
{
    //
    // 摘要:
    //     指定打印队列的属性。
    [Flags]
    public enum EnumeratedPrintQueueTypes
    {
        //
        // 摘要:
        //     允许在队列中的多个打印作业打印队列。
        Queued = 1,
        //
        // 摘要:
        //     打印作业将直接发送到打印而不是第一次后台处理作业的打印队列。
        DirectPrinting = 2,
        //
        // 摘要:
        //     共享打印队列。
        Shared = 8,
        //
        // 摘要:
        //     连接到指定的打印服务器打印队列。
        Connections = 16,
        //
        // 摘要:
        //     安装为指定的打印服务器上的本地打印队列打印队列。
        Local = 64,
        //
        // 摘要:
        //     文档和打印机配置不匹配时将保留其打印作业打印队列。
        EnableDevQuery = 128,
        //
        // 摘要:
        //     打印它们之后将作业保存在队列中打印队列。
        KeepPrintedJobs = 256,
        //
        // 摘要:
        //     打印队列可脱机工作。
        WorkOffline = 1024,
        //
        // 摘要:
        //     已启用双向通信的打印机打印队列。
        EnableBidi = 2048,
        //
        // 摘要:
        //     只有原始数据后台处理打印队列。
        RawOnly = 4096,
        //
        // 摘要:
        //     是可见的目录中的打印机的打印队列。
        PublishedInDirectoryServices = 8192,
        //
        // 摘要:
        //     服务于传真机打印队列。
        Fax = 16384,
        //
        // 摘要:
        //     安装终端服务中的重定向功能的打印队列。
        TerminalServer = 32768,
        //
        // 摘要:
        //     使用推入打印机连接用户策略安装打印队列。 请参阅“备注”。
        PushedUserConnection = 131072,
        //
        // 摘要:
        //     使用推入打印机连接计算机策略安装打印队列。 请参阅“备注”。
        PushedMachineConnection = 262144
    }
}

2 使用 InstalledPrinters

使用 PrinterSettings 类的 InstalledPrinters,获取在计算机上安装的所有打印机的名称。

foreach (string printer in PrinterSettings.InstalledPrinters) {
    Console.WriteLine(printer);
}
最后更新 2020-09-13
MIP.watch('startSearch', function (newVal, oldVal) { if(newVal) { var keyword = MIP.getData('keyword'); console.log(keyword); // 替换当前历史记录,新增 MIP.viewer.open('/s/' + keyword, {replace: true}); setTimeout(function () { MIP.setData({startSearch: false}) }, 1000); } }); MIP.watch('goHome', function (newVal, oldVal) { MIP.viewer.open('/', {replace: false}); });