不打开就打印文件 [英] Print Files without opening

查看:154
本文介绍了不打开就打印文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中有一个或多个文件.当我单击asp.net中的打印"按钮时,应该在不打开文件的情况下打印该文件夹中的所有文件.

如何在ASP.NET中做到这一点.

I have one or more files in a folder. When i click on Print button in asp.net, I should print all the files from this folder without opening them.

How can I do this in ASP.NET.

推荐答案

我正在使用以下代码进行打印:

受保护的无效btnPrint_Click(对象发送者,EventArgs e)
{
字符串路径= Server.MapPath(〜/Documents/");

//从目录中获取所有文件
字符串[] docFiles = Directory.GetFiles(path);

//遍历文件
for(int count = 0; count< docFiles.Length; count ++)
{
PrintFiles(docFiles [count]);
}
}

void PrintFiles(string fileToPrint)
{
Process pro = new Process();
ProcessStartInfo info =新的ProcessStartInfo();
info.CreateNoWindow = false;
info.FileName = string.Format("PRINT \\\\ 10.138.77.54 \\ HP LaserJet 5000 Series PCL6",fileToPrint);
pro.StartInfo = info;//这里抛出异常
pro.Start();
}

Win32Exception未通过用户代码处理.系统找不到指定的文件.

它正在尝试打印的文件在那里.在这里,打印机位于不是本地计算机的服务器上.请在下一行告诉我如何获取打印机参考并发出打印命令.


info.FileName = string.Format("PRINT \\\\ 10.138.77.54 \\ HP LaserJet 5000 Series PCL6",fileToPrint);
I am using below code for printing :

protected void btnPrint_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/Documents/");

//Get all the files from the Directory
string[] docFiles = Directory.GetFiles(path);

//Loop through the files
for (int count = 0; count < docFiles.Length; count++)
{
PrintFiles(docFiles[count]);
}
}

void PrintFiles(string fileToPrint)
{
Process pro = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = false;
info.FileName = string.Format("PRINT \\\\10.138.77.54\\HP LaserJet 5000 Series PCL6", fileToPrint);
pro.StartInfo = info;//Exception is throw here
pro.Start();
}

Win32Exception was unhandled by user code. The system cannot find the file specified.

The file it is trying to print is there. Here the printer is on server which is not the local machine. Please tell me in the following line how can i get the reference to the printer and give the print command.


info.FileName = string.Format("PRINT \\\\10.138.77.54\\HP LaserJet 5000 Series PCL6", fileToPrint);


请不要按"answer"以发布更多问题.编辑您的帖子.如您所说,您的代码只能使用服务器上的打印机打印服务器上的文档.鉴于这对于任何类型的Web应用程序都是毫无价值的,并且需要在本地网络中运行才能完全有意义,所以我建议您不想使用您的Web应用程序来执行此操作您的网络应用会触发执行此操作的服务,或者只是编写一些代码来执行该服务.

我确实认为您需要检查正在生成的字符串,学习使用@来构建可读的字符串,而且我也不认为该字符串.您根本不需要添加fileToPrint,而是将其添加在引号中.我认为您的核心问题很可能就是您没有费心去调试代码,并且FileName完全错误.
Please don''t push ''answer'' to post more questions. Edit your post. As you say, your code can only ever print documents that are on the server, using a printer that is on the server. Given that this is utterly worthless for any sort of web app, and would need to be run inside a local network to make any sense at all, I suggest you don''t want to use your web app to do this, you could have your web app trigger a service that does it, or just write some code to do it.

I do think you need to check the string you''re generating, learn to use @ to build strings that are readable, and I also don''t think that string.Format is doing what you expect here. You''re not adding fileToPrint at all, let along adding it in quotes. I think your core issue may well be just that you haven''t bothered to debug your code, and your FileName is totally wrong.


这篇关于不打开就打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆