2012-06-19 45 views
1

我想讀取所有文件的文件夾並以編程方式打印它們,就像我右鍵單擊>打印一樣。以編程方式右鍵單擊並打印文件

我知道打印這種方式是默認應用程序特定的。所以我認爲這是一個兩步驟的過程:如何檢查具有默認應用程序的文件是否支持打印;以及我如何實際發出打印文件的命令?

正在打印這種叫做「shell命令打印」的東西嗎?將需要正確的詞彙來搜索信息。

任何其他更好的方式,你會建議這項任務?

編輯:文件類型可以是簡單的.txt文件以外的任何其他文件,例如, PDF,DWG,JPEG等

回答

1

可以使用Directory.GetFiles枚舉文件夾中的文件,然後使用的Process.Start的ShellExecute的模式,在每個依次文件執行「打印」動詞(命令)。

請參閱Process.Start here,並且您需要傳遞ProcessStartInfo,其中UseShellExecuteVerb設置得當。

通過詢問操作系統以解決如何打印出來,你不用擔心的複雜性如何打印不同類型的數據,等等

+0

你能告訴我們Shell命令的語法嗎? –

+1

@JeremyThompson'print/d:\\ printserver \ sharenamedrive:\ path \ filename'根據Rohan Durve在他的回答中的鏈接。 – Jake

1

我相信這是你在找什麼:

http://support.microsoft.com/kb/314499

如果不工作,那麼還有很多其他方法使用宏來做到這一點或通過編寫一個非常簡單的視覺基本程序來爲你做。

評論如果這不起作用,我會編輯我的帖子。

問候〜

+0

這種方法已經通過我對所有被測試打印機直接支持的文件類型。 –

-1

As you said: You want to read files in a folder and automatically print them

所以,一個選擇是讀取文件>打開的FileStream和發送打印流。這裏是一個打印流中的樣品 -

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx

通知的Printing()功能於樣品中。 [我還沒有嘗試過,但看起來實現]

// Print the file. 
    public void Printing() 
    { 
     try 
     { 
      streamToPrint = new StreamReader (filePath); 
      try 
      { 
       printFont = new Font("Arial", 10); 
       PrintDocument pd = new PrintDocument(); 
       pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 
       // Print the document. 
       pd.Print(); 
      } 
      finally 
      { 
       streamToPrint.Close() ; 
      } 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

這裏是多一個選擇 -http://channel9.msdn.com/forums/TechOff/151242-How-to-send-a-PDF-to-a-printer/ --- See the last post

相關問題