c# - 进程总是通过默认打印机打印文档

标签 c# printing

我在选择打印机打印文档时遇到问题。

我的代码是:

var filename = @"C:\Users\I\Desktop\test.doc";

PrintDialog pd = new PrintDialog();

pd.PrinterSettings =new PrinterSettings();

    if (DialogResult.OK == pd.ShowDialog(this))
    {
        Process objP = new Process();

        objP.StartInfo.FileName = filename;


        objP.StartInfo.WindowStyle =

        ProcessWindowStyle.Hidden; //Hide the window. 

        objP.StartInfo.Verb ="print";

          objP.StartInfo.Arguments ="/p /h \"" + filename + "\" \"" + pd.PrinterSettings.PrinterName + "\"";
        objP.StartInfo.CreateNoWindow = false;
        //true;//!! Don't create a Window. 
        objP.Start();
        //!! Start the process !!// 
        objP.CloseMainWindow();
    }

无论我选择什么,process 总是会使用默认打印机,无论 pd.PrinterSettings.PrinterName 的值是什么。

我的代码有什么问题?

最佳答案

您可能想使用“PrintTo”而不是“print”作为动词。您已经将 objP.FileName 设置为文件名,因此无需在参数中变得复杂。在那里单独传递打印机名称。

var filename = @"C:\Users\I\Desktop\test.doc"; 

PrintDialog pd = new PrintDialog(); 

pd.PrinterSettings =new PrinterSettings(); 

if (DialogResult.OK == pd.ShowDialog(this)) 
{ 
    Process objP = new Process(); 

    objP.StartInfo.FileName = filename;
    objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window.
    objP.StartInfo.Verb ="PrintTo";
    objP.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
    objP.StartInfo.CreateNoWindow = false; 
    //true;//!! Don't create a Window.  

    objP.Start(); 
    //!! Start the process !!//  

    objP.CloseMainWindow(); 
}

关于c# - 进程总是通过默认打印机打印文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8423231/

相关文章:

javascript - 在javascript中打印div内容

java - 打印无法正常工作

c# - System.Printing 未找到(C#)?

javascript - ASP.NET 中的选项卡式网站,想要打印所有内容,有推荐吗?

c# - 需要将 C# 字符串传递给 javascript 函数

javascript - 如何使用 ActiveX 在 IE 11 中直接打印

c# - 你如何在 Gtk Sharp 中制作语法高亮文本编辑器?

c# - 如何防止 MyISAM 大表崩溃

c# - 生产者消费者模式如何通知完成?

c# - 使用 XmlSerializer 自定义序列化