2011-10-22 197 views
0

如何通過打印機自動打印基於給定文件名的文本文件,而無需在C sharp窗口服務中執行任何手動手動工作,它不是爲我工作,任何人給我建議。如何通過使用C#窗口服務通過打印機打印數據打印文本文件

using System.Management; 

    private Font printFont; 
    private StreamReader streamToPrint; 

private void GetPrinters(string fileName) 
    { 

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); 

     string printerName = ""; 
     foreach (ManagementObject printer in searcher.Get()) 
     { 
     printerName = printer["Name"].ToString().ToLower(); 
     if (printerName.Equals(@"\\chenraqdc1.raqmiyat.local\hp laserjet black chennai")) 
      { 
       Console.WriteLine("Printer = " + printer["Name"]); 
       if (printer["WorkOffline"].ToString().ToLower().Equals("true")) 
        { 
         // printer is offline by user 
         label1.Text = "Your Plug-N-Play printer is not connected."; 
        } 
       else 
        {streamToPrint = new StreamReader(fileName); 

        printFont = new Font("Arial", 10); 
        PrintDocument pd = new PrintDocument(); 
        pd.PrintPage += new PrintPageEventHandler 
         (this.pd_PrintPage); 
        pd.Print(); 

        streamToPrint.Close(); 
        } 
      } 
     } 
    } 
      private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
     { 
     float linesPerPage = 0; 
     float yPos = 0; 
     int count = 0; 
     float leftMargin = ev.MarginBounds.Left; 
     float topMargin = ev.MarginBounds.Top; 
     string line = null; 

     // Calculate the number of lines per page. 
     linesPerPage = ev.MarginBounds.Height/
      printFont.GetHeight(ev.Graphics); 

     // Print each line of the file. 
     while (count < linesPerPage && 
      ((line = streamToPrint.ReadLine()) != null)) 
     { 
      yPos = topMargin + (count * 
       printFont.GetHeight(ev.Graphics)); 
      ev.Graphics.DrawString(line, printFont, Brushes.Black, 
       leftMargin, yPos, new StringFormat()); 
      count++; 
     } 
+0

我是否需要設置任何打印機設置,y它沒有得到打印 – pravz

+0

代碼是不會拋出任何錯誤,pd.Print();系統來到這條線它顯示一個小的對話框打印文件,但文件沒有得到打印,任何方式善意地幫助如何禁用該打印對話框 – pravz

回答

0

你可以做這樣的事情......

注:這是示例代碼如何使用C#窗口服務打印PDF文件,如果你要打印的文本文件,你可以更改此代碼

帶有按鈕(cmdGetPrinters)和ListView的Windows窗體 (lstPrinters)。列表視圖有兩列定義 - 「屬性」和 「值」,它將描述安裝在本地 機器上的打印機。下面的代碼實現了魔術。

using System;                
using System.Collections.Generic;           
using System.ComponentModel;             
using System.Data; System.Drawing;           
using System.Text; System.Windows.Forms;         
using System.Management;             
using System.Management.Instrumentation;          

public partial class frmPrintDisplay : Form         
{                   
public frmPrintDisplay()            
{                  
     InitializeComponent();            
}                  

private void cmdGetPrinters_Click(object sender, EventArgs e)   
{                  
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer"); 

    ManagementObjectSearcher mo = new ManagementObjectSearcher(query); 

    ManagementObjectCollection printers = mo.Get();      

    foreach (ManagementObject printer in printers)      
    {                 
     PropertyDataCollection printerProperties = printer.Properties; 
     string printerPath = printer.Path.ToString() ;     
     PropertyDataCollection.PropertyDataEnumerator test =    
      printer.Properties.GetEnumerator();       

     while(! (test.MoveNext()== false))        
     {                
      lstPrinters.Items.Add(          
       new ListViewItem(new string[]       
       {              
        test.Current.Name,         
        (             
         (test.Current.Value == null) ?     
         "n/a" : test.Current.Value.ToString()   
        )             
       })              
      );               
     }                
     }                 
    }                  
    } 

此圖顯示了這個小窗口應用程序的結果。請注意「名稱」屬性,這將用於將文本文件發送到打印機。這顯示在打印機的「ShareName」屬性下。在同一個域/工作組中的測試機器上,您必須安裝新的網絡打印機,並指出安裝情況以查看第一臺計算機上的共享打印機。這實際上使得第一臺計算機成爲了打印機服務器,並且您可以爲客戶端重新設置設置。

現在到測試機......應用上述code building保存文本文件到一個臨時目錄名爲C:\ Program Files文件[應用程序名稱] \ TEMP \

using System;                
    using System.Collections.Generic;           
    using System.ComponentModel;             
    using System.Data; System.Drawing;           
    using System.IO;               
    using System.Text; System.Windows.Forms;         
    using System.Management;             
    using System.Management.Instrumentation;          



private void print(ref DirectoryInfo tempDir)        
{                   
    try                  
    {                  
    foreach(FileInfo file in tempDir.GetFiles("*.pdf"))     
    {                 
     file.CopyTo("\\\\XYZ\\Phaser77\\" + file.Name);     
    }                 
    }                  
    catch(Exception ee){ }             
}  

我希望這將有助於你

相關問題