注意要引用System.Management.dll (.net组件下)
Code
/// <summary>
/// 获取本地当前默认打印机
/// </summary>
/// <returns></returns>
public static string GetDefaultPrinter()
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from Win32_Printer");
string DefaultPrinter="";
foreach (ManagementObject Printer in searcher.Get())
{
if (bool.Parse(Printer["Default"].ToString()))
{
DefaultPrinter=Printer["Name"].ToString();
break;
}
}
return DefaultPrinter;
}
/// <summary>
/// 设置本地当前默认打印机
/// </summary>
/// <returns></returns>
public static void SetDefaultPrinter(string PrinterName)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from Win32_Printer");
foreach (ManagementObject Printer in searcher.Get())
{
if (string.Compare(Printer["Name"].ToString(),PrinterName,true)==0)
{
Printer.InvokeMethod("SetDefaultPrinter",null);
break;
}
}
}
/// <summary>
/// 获取本地当前默认打印机
/// </summary>
/// <returns></returns>
public static string GetDefaultPrinter()
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from Win32_Printer");
string DefaultPrinter="";
foreach (ManagementObject Printer in searcher.Get())
{
if (bool.Parse(Printer["Default"].ToString()))
{
DefaultPrinter=Printer["Name"].ToString();
break;
}
}
return DefaultPrinter;
}
/// <summary>
/// 设置本地当前默认打印机
/// </summary>
/// <returns></returns>
public static void SetDefaultPrinter(string PrinterName)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select * from Win32_Printer");
foreach (ManagementObject Printer in searcher.Get())
{
if (string.Compare(Printer["Name"].ToString(),PrinterName,true)==0)
{
Printer.InvokeMethod("SetDefaultPrinter",null);
break;
}
}
}