Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

C#
How to send request from client to server printer for printing using c# .net windows application?


I want to build application for printing document when printer installed in server.

I am using windows service on server machine when user send file from client to server in a spacific folder so, windows service will be pick and print the file from that folder.But my document did not print.

I am using this code for printing windows service.

Thanks in Advance.
Please help me.

Ankit Agarwal
Software Engineer

What I have tried:

C#
string schTime;
        string _lastTime;
        Timer timer1 = new Timer();
        protected override void OnStart(string[] args)
        {
            try
            {
                //Debugger.Break();

                TraceService("Start Service" + " " + DateTime.Now.ToString());
                timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                timer1.Enabled = true;
                //timer1.Interval = 1000;
                //string[] files = Directory.GetFiles(@"C:\PrintingDocument\");
                //foreach (string file in files)
                //{
                //    if (File.Exists(file))
                //    {
                        //timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                        //timer1.Enabled = true;
                //    }
                //    else
                //    {
                //        TraceService("File did not exists");
                //    }
                //}
                //Debugger.Break();
            }
            catch (Exception ex)
            {
                TraceService("Error: " + ex);
                //EventLog.WriteEntry("Error: " + ex.Message);
            }
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            try
            {

                //timer1.Stop();
                //Debugger.Break();
                //if (!schTime.Contains(DateTime.Now.ToString("HH:mm")) || DateTime.Now.ToString("HH:mm") == _lastTime)
                //    return;
                //_lastTime = DateTime.Now.ToString("HH:mm");
                
                timer1.Enabled = false;
                TraceService("Another entry at" + " " + DateTime.Now);
                if (!Directory.Exists(@"C:\PrintingDocument"))
                    Directory.CreateDirectory(@"C:\PrintingDocument");
                string[] files = Directory.GetFiles(@"c:\PrintingDocument\");
                foreach (string file in files)
                {
                    //if (string.IsNullOrEmpty(file))
                    //{
                    //    //txtFileName.BackColor = Color.Yellow;
                    //    //MessageBox.Show("Please Select file.");
                    //    return;
                    //}
                    ProcessStartInfo info = new ProcessStartInfo(file);
                    info.Verb = "Print";
                    info.CreateNoWindow = true;
                    info.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(info);
                    TraceService(@"C:\PrintingDocument\" + file);
                    TraceService(@"C:\PrintedDocument\" + file);
                    System.IO.File.Move(@"C:\PrintingDocument\" + file, @"C:\PrintedDocument\" + file);
                    

                }
                TraceService("Printing Sucessfully" + " " + DateTime.Now.ToString());
                
                
                timer1.Enabled = true;
                //WindowsServiceSchedular(timer1);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Error: " + ex.Message);
                TraceService("Error: " + ex);
            }
        }
       
        private void TraceService(string content)
        {
            try
            {

                if (!Directory.Exists(@"C:\PrintingStatus"))
                    Directory.CreateDirectory(@"C:\PrintingStatus");
                FileStream fs = new FileStream(@"C:\PrintingStatus\PrintingStatus.Txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.WriteLine(content);
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Error: " + ex.Message);
            }
        }
        protected override void OnStop()
        {
            timer1.Stop();
            TraceService("Stop Service");
        }
Posted
Updated 15-Oct-16 2:22am
Comments
Richard MacCutchan 15-Oct-16 7:01am    
Why not just make the printer a shared device and let Windows do all the work?
Agarwal1984 15-Oct-16 7:14am    
But my printer code did not run without any error.
foreach (string file in files)
{
ProcessStartInfo info = new ProcessStartInfo(file);
info.Verb = "Print";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
TraceService(@"C:\PrintingDocument\" + file);
TraceService(@"C:\PrintedDocument\" + file);
System.IO.File.Move(@"C:\PrintingDocument\" + file, @"C:\PrintedDocument\" + file);


}
Printer is not printing the document.
[no name] 15-Oct-16 7:21am    
It's time to put those "senior developer" powers to work and find out why you docs aren't printing. This has nothing to about sending requests from any client to a server. Learn how to debug your code.
Agarwal1984 15-Oct-16 7:24am    
I am only a developer of .Net.

1 solution

An article right here on CodeProject:

An absolute beginner's guide to printing in .NET[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900