COM方式:使用Winspool.drv
WMI方式:Class=Win32_ Printer 另注(Powershell中可以 gwmi -Class "Win32_Printer"找到List )
C#实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Runtime.InteropServices;
namespace MyConsoleApp.Code
{
/// <summary>
/// 设置或者获取默认打印机类
/// </summary>
public class PrintHelper
{
#region DllImport
[DllImport("Winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string printerName);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int pcchBuffer);
public static string GetDefaultPrinter()
{
const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_INSUFFICIENT_BUFFE

本文介绍了两种在C#中设置默认打印机的方法:一是利用COM组件Winspool.drv,二是通过WMI(Windows Management Instrumentation)。在WMI方式中,主要操作Class为Win32_Printer,并提及在Powershell中可以使用gwmi命令来获取打印机列表。文章详细讲解了C#实现这两种方法的步骤和示例代码。
最低0.47元/天 解锁文章
5754

被折叠的 条评论
为什么被折叠?



