为安装的应用程序分配单独的默认打印机

时间:2018-10-29 06:57:08

标签: c# printing

我想要为已安装的应用程序分配单独的默认打印机。我是否必须使用regedit进行此操作,或者还有另一种方法。可以使用C#编写此应用程序吗?作为打印机的应用程序开发人员,我应该知道些什么?

https://www.symantec.com/connect/downloads/automatically-change-default-printer-different-programs

 private void FrmPrinter_Load(object sender, EventArgs e)
    {
        // Installed printers
        foreach (string printer in PrinterSettings.InstalledPrinters)
        {
            CmbPrinters.Items.Add(printer);
        }

        // Default printer
        PrintDocument pd = new PrintDocument();
        string defPrinter = pd.PrinterSettings.PrinterName;

        LblDefPrinter.Text = defPrinter;
    }

    private void CmbPrinters_SelectedValueChanged(object sender, EventArgs e)
    {
        // Computer default printer assignment
        string pname = this.CmbPrinters.SelectedItem.ToString();
        Printers.SetDefaultPrinter(pname);
    }

    private void BtnFile_Click(object sender, EventArgs e)
    {
        // I want to assign a printer for the application selected here.
        OpenFileDialog file = new OpenFileDialog();
        file.Filter = "Exe Dosyası |*.exe";  
        file.FilterIndex = 2;
        file.RestoreDirectory = true;
        file.CheckFileExists = false;
        file.Title = "Exe dosyası seçiniz..";
        file.Multiselect = true;

        if (file.ShowDialog() == DialogResult.OK)
        {
            string fileName = file.SafeFileName;
            LblFileName.Text = fileName;
        }
    }
}

public static class Printers
{
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetDefaultPrinter(string Name);

}

0 个答案:

没有答案