如何使用C#.NET Windows应用程序从客户端向服务器打印机发送请求以进行打印? [英] How to send request from client to server printer for printing using C# .NET windows application?
本文介绍了如何使用C#.NET Windows应用程序从客户端向服务器打印机发送请求以进行打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
如何从客户端发送请求服务器打印机 打印使用 c#.net windows应用程序?
当打印机安装在服务器上时,我想构建打印文档的应用程序。
我在服务器机器上使用Windows服务当用户将文件从客户端发送到空间文件夹中的服务器时,Windows服务将从该文件夹中选择并打印文件。但是我的文档没有打印。
我正在使用此代码打印Windows服务。
先谢谢。
请帮帮我。
Ankit Agarwal
软件工程师
我的尝试:
string schTime;
string _lastTime;
定时器timer1 = new Timer();
受保护 覆盖 void OnStart ( string [] args)
{
try
{
// Debugger.Break();
TraceService(< span class =code-string> 启动服务 + + DateTime.Now.ToString());
timer1.Elapsed + = new ElapsedEventHandler(OnElapsedTime);
timer1.Enabled = true ;
// timer1.Interval = 1000;
// string [] files = Directory.GetFiles(@C:\ PrintingDocument \);
< span class =code-comment> // foreach(文件中的字符串文件)
// {
// if(File.Exists(file))
// {
// timer1.Elapsed + = new ElapsedEventHandler(OnElapsedTime);
// timer1.Enabled = true;
// }
// else
// {
// TraceService(文件不存在);
< span class =code-comment> // }
// }
// Debugger.Break() ;
}
catch (例外情况)
{
TraceService( 错误: + ex);
// EventLog.WriteEntry(Error:+ ex.Message);
}
}
私有 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( + 的另一个条目 + DateTime.Now);
if (!Directory.Exists( @ C:\ PrintingDocument))
Directory.CreateDirectory( @ C:\\ \\PrintingDocument跨度>);
string [] files = Directory.GetFiles( @ C:\PrintingDocument\跨度>);
foreach (字符串文件 in 文件)
{
// if(string.IsNullOrEmpty(file))
// {
// //txtFileName.BackColor = Color.Yellow;
// //MessageBox.Show(请选择文件。);
// 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( 成功打印 + + DateTime.Now.ToString());
timer1.Enabled = true ;
// WindowsServiceSchedular(timer1);
}
catch (Exception ex)
{
EventLog.WriteEntry( 错误: + ex.Message);
TraceService( 错误: + ex);
}
}
私有 void TraceService( 字符串内容)
{
尝试
{
if (!Directory.Exists( @ C:\ PrrintStatus))
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 (例外情况)
{
EventLog.WriteEntry( 错误: + ex.Message);
}
}
受保护 覆盖 void OnStop()
{
timer1.Stop();
TraceService( 停止服务);
}
解决方案
关于CodeProject的文章:
绝对新手的.NET打印指南 [ ^ ]
Hello,
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:
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");
}
解决方案
An article right here on CodeProject:
An absolute beginner's guide to printing in .NET[^]
这篇关于如何使用C#.NET Windows应用程序从客户端向服务器打印机发送请求以进行打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文