C# 进程始终使用默认打印机打印文档
我在选择打印机打印文档时遇到问题 我的代码是:C# 进程始终使用默认打印机打印文档,c#,printing,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();
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
的值是多少
我的代码有什么问题?尝试更改
pd.PrinterSettings=new PrinterSettings()代码>要阅读以下内容:
pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings;
默认情况下,当您创建打印机设置实例时,它会返回默认打印机名称,仅供参考。。。然后你可以试试这样的东西
//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
}
你可能想用“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();
}
objP.StartInfo.Arguments
的值是否是您在单步执行此代码时所期望的值?当您在命令提示下手动输入时,这是否有效?