关闭

C#修改系统默认打印机

1126人阅读 评论(0) 收藏 举报
分类:
转自: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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:260445次
    • 积分:3562
    • 等级:
    • 排名:第9545名
    • 原创:65篇
    • 转载:157篇
    • 译文:0篇
    • 评论:23条
    文章分类
    最新评论