选择打印机打印文档时遇到问题。
我的代码是:
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
的值是多少。
我的代码出了什么问题?
答案 0 :(得分:3)
您可能希望使用“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();
}
答案 1 :(得分:2)
尝试更改pd.PrinterSettings =new PrinterSettings();
以阅读以下内容:
pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings;
默认情况下,当您创建打印机设置实例时,它会返回默认的打印机名称,只是一个fyi ...您可以尝试这样的事情
//sudu code
foreach(string strPrinter in PrinterSettings.InstalledPrinters)
{
// or unless you know the name of the printer then skip this and assign it to the code above
}