2011-12-07 94 views
4

我在選擇打印機打印文檔時遇到問題。進程始終默認打印文檔打印機

我的代碼是:

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

「objP.StartInfo.Arguments」的值是否是您在執行此代碼時所期望的值?當你在命令提示符下手動鍵入時,它是否工作正常? –

回答

2

嘗試改變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 
} 
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(); 
} 
相關問題