C#修改系统默认打印机

转自:http://zhidao.baidu.com/link?url=-LtSl1N4OBt3Q3C9XPHvA6TDs8lSazEfdZxxiJZ1rOTwpz6otAZ4oolVhsi0CCDlMLdF1VsLZO6TgusNs_0miq
实例化winspool.drv对象,然后调用SetDefaultPrinter方法来设置默认打印机
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
       private static extern long SetDefaultPrinter(string pszPrinter);
追问
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
{
    public partial class Form1 : Form
    {public Form1()
        { InitializeComponent(); }
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
private void button1_Click(object sender, EventArgs e) {SetDefaultPrinter("My Microsoft XPS Document Writer");}}

c# 设置默认打印机

using System;
using System.Management;             //需要引用System.Management

public class Sample
{
     public static void Main(string[] args)
     {
         string PrinterName = "Adobe PDF";
         ManagementObjectSearcher query;
         ManagementObjectCollection queryCollection;
         string _classname = "SELECT * FROM Win32_Printer";

         query = new ManagementObjectSearcher(_classname);
         queryCollection = query.Get();

         foreach (ManagementObject mo in queryCollection)
         {
             if (string.Compare(mo["Name"].ToString(), PrinterName, true) == 0)
             {
                 mo.InvokeMethod("SetDefaultPrinter", null);
                 break;
             }
         }

         foreach (ManagementObject mo in queryCollection)
         {
                 Console.WriteLine(mo["Name"].ToString());
         }

        Console.ReadLine();
     }
}

转自:http://hi.baidu.com/jackeyrain/item/5ddf777065edb946ee1e53c4

展开阅读全文

没有更多推荐了,返回首页