using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace PrintAgent
{
    public partial class MyNewService : ServiceBase
    {
        public MyNewService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
            

        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart.");

            //string directory = Directory.GetCurrentDirectory();
            string directory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            string path = System.IO.File.ReadAllText(string.Format(@"{0}\\path.txt",directory));

            if (File.Exists(path))
            {
                // This path is a file
                eventLog1.WriteEntry("File.Exists");
                ProcessFile(path);
            }
            else if (Directory.Exists(path))
            {
                // This path is a directory
                eventLog1.WriteEntry("Directory.Exists");
                ProcessDirectory(path);
            }
            else
            {
                eventLog1.WriteEntry(String.Format("{0} is not a valid file or directory.", path));
            }

        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("In OnStop.");
        }

        protected override void OnContinue()
        {
            eventLog1.WriteEntry("In OnContinue.");
        }


        // Process all files in the directory passed in, recurse on any directories 
        // that are found, and process the files they contain.
        public void ProcessDirectory(string targetDirectory)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(targetDirectory);
            foreach (string fileName in fileEntries)
            {
                ProcessFile(fileName);
            }
            // Recurse into subdirectories of this directory.
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
            foreach (string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory);
        }

        // Insert logic for processing found files here.
        public void ProcessFile(string path)
        {
            //Console.WriteLine("Processed file '{0}'.", path);
            //Console.ReadLine();

            eventLog1.WriteEntry("Printing " + path);

            print(path);
        }

        public void print(string filename)
        {

            Process p = new Process();
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = filename;
            p.StartInfo.Verb = "print";
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.Start();
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            if (p.HasExited == false)
            {
                p.WaitForExit(10000);
            }

            p.EnableRaisingEvents = true;
            //p.CloseMainWindow();
            p.Close();

        }

    }
}
 



دانلود فایل


مطلبی هم تو این وبلاگ پیدا کردم که به نوعی دیگه داره همین کار را با Web Api و C++ انجام میده که خیلی خیلی قشنگه :

http://www.strathweb.com/2012/10/remote-printing-with-asp-net-web-api-in-windows-service