使用c#windows服务打印文件

问题描述:

我想使用windows server自动打印目录中的文件。一旦任何文件被添加到目录,它应该会自动打印。我们怎样才能做到这一点?使用c#windows服务打印文件

谢谢。

我还没有测试从另一个线程打印,但这两个选项之一应该工作。您可能需要修改代码与.net 2的工作,因为我只用3.5 SP1或4

假设你有一个打印方法和队列,其中ItemToPrint是类持有的打印设置/信息

public Queue<ItemToPrint> PrintQueue = new Queue<ItemToPrint>(); 
    private BackgroundWorker bgwPrintWatcher; 

    public void SetupBackgroundWorker() 
    { 
     bgwPrintWatcher = new BackgroundWorker(); 
     bgwPrintWatcher.WorkerSupportsCancellation = true; 
     bgwPrintWatcher.ProgressChanged += new ProgressChangedEventHandler(bgwPrintWatcher_ProgressChanged); 
     bgwPrintWatcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwPrintWatcher_RunWorkerCompleted); 
     bgwPrintWatcher.DoWork += new DoWorkEventHandler(bgwPrintWatcher_DoWork); 
     bgwPrintWatcher.RunWorkerAsync(); 
    } 

    void bgwPrintWatcher_DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 
     while (!worker.CancellationPending) 
     { 
      // Prevent writing to queue while we are reading/editing it 
      lock (PrintQueue) 
      { 
       if (PrintQueue.Count > 0) 
       { 
        ItemToPrint item = PrintQueue.Dequeue(); 
        // Two options here, you can either sent it back to the main thread to print 
        worker.ReportProgress(PrintQueue.Count + 1, item); 
        // or print from the background thread 
        Print(item); 
       } 
      } 
     } 
    } 

    private void Print(ItemToPrint item) 
    { 
     // Print it here 
    } 

    private void AddItemToPrint(ItemToPrint item) 
    { 
     lock (PrintQueue) 
     { 
      PrintQueue.Enqueue(item); 
     } 
    } 

    void bgwPrintWatcher_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     // Anything here will run from the main/original thread 
     // PrintQueue will no longer be watched 
    } 

    void bgwPrintWatcher_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     // Anything here will run from the main/original thread 
     ItemToPrint item = e.UserState as ItemToPrint; 
     // e.ProgressPercentage holds the int value passed as the first param 
     Print(item); 
    } 

您可以使用FileSystemWatcher类。

Description here