简体   繁体   English

通过在Windows服务中执行进程进行打印

[英]Printing by executing a process in a Windows Service

I have a Windows Service that needs to start a process to send a file to the printer (I found that solution there https://stackoverflow.com/a/4875755/1228738 ) . 我有一个Windows服务需要启动一个过程来发送文件到打印机(我在那里找到了解决方案https://stackoverflow.com/a/4875755/1228738 )。 I do this using the Process.Start(). 我使用Process.Start()来做到这一点。 My problem is that nothing happens. 我的问题是没有任何反应。

The service is actually installed on my developer machine (win7, x64). 该服务实际上安装在我的开发人员计算机上(win7,x64)。 I tried installing it as LOCAL SYSTEM, NETWORK SERVICE, LOCAL SERVICE with the same result every time. 我尝试将其作为LOCAL SYSTEM,NETWORK SERVICE,LOCAL SERVICE安装,每次都有相同的结果。

I tried those way of starting my process : 我尝试了这种方式来启动我的过程:

Process p = new  Process();
p.StartInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
p.StartInfo.Arguments = "-p myFile.pdf";
p.Start();

and

Process.Start("C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe", "-p myFile.pdf");

and also 并且

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
startInfo.Arguments = "-p myFile.pdf";

Process.Start(startInfo);

When I execute the same code in a winform application, everything works fine, the file is sent to the printer. 当我在winform应用程序中执行相同的代码时,一切正常,文件被发送到打印机。 But in the Windows Service, nothing happens. 但在Windows服务中,没有任何反应。

I saw that post https://stackoverflow.com/a/6271309/1228738 , which explains why I would not see the UI, that's fine I don't have any UI anyway. 我看到了帖子https://stackoverflow.com/a/6271309/1228738 ,这解释了为什么我不会看到用户界面,这很好我无论如何都没有任何用户界面。 But as said in the comment section, a process with no user input should be OK. 但正如评论部分所述,没有用户输入的过程应该没问题。 The process that I start don't need any user input. 我开始的过程不需要任何用户输入。

The only thing I can think of right now, is that because of session isolation ( https://stackoverflow.com/a/5063750/1228738 ), the service can't find any installed printers... Can that be the case ? 我现在唯一能想到的是,由于会话隔离( https://stackoverflow.com/a/5063750/1228738 ),该服务无法找到任何已安装的打印机......可以是这种情况吗? If so, any suggestion how to work around that ? 如果是这样,任何建议如何解决? And if not, any idea of what's wrong ? 如果没有,任何错误的想法?

Thanks! 谢谢!

EDIT #1 编辑#1

I tried running the service with my user account, and it's working, so I guess my fears are confirmed... the users LOCAL SYSTEM and NETWORK SERVICE have no installed printers. 我尝试使用我的用户帐户运行该服务,它正在运行,所以我猜我的担心得到了确认......用户LOCAL SYSTEM和NETWORK SERVICE没有安装打印机。

So I'll refine my question a little bit. 所以我会稍微改进一下我的问题。 Is there a way for those account to access printers installed on the computer ? 这些帐户有没有办法访问计算机上安装的打印机?

EDIT #2 编辑#2

We finally decided that a user will be created for running that service and in that user accounts we'll install the printer on which to print. 我们最终决定创建一个用户来运行该服务,在该用户帐户中我们将安装要打印的打印机。

I guess this question can be closed now. 我想这个问题现在可以关闭了。 Thank you all for your help. 感谢大家的帮助。

I had this issue too, this trick solved it 我也有这个问题,这个技巧解决了它

Go to services ---> Double click the required service ---> proceed to logon tab 转到服务--->双击所需的服务--->继续登录选项卡

Supply the Log-in credentials from which printer was installed. 提供安装打印机的登录凭据。

Run your service, then check the printer queue. 运行您的服务,然后检查打印机队列。

Reason: Local system account does not have those printer installed ! 原因:本地系统帐户没有安装这些打印机!

See screen shot below. 见下面的屏幕截图。 在此输入图像描述

The solution here is tho share your local printer and call Foxit with 这里的解决方案是分享您的本地打印机并与Foxit联系

-/t yourfile.pdf \\\\localhost\\YourSharedPrinter

That way your service does not need an UserProfile and no DefaultPrinter. 这样,您的服务不需要UserProfile,也不需要DefaultPrinter。

Check out this MSDN Page: http://support.microsoft.com/kb/324565 查看此MSDN页面: http//support.microsoft.com/kb/324565

According to this page, you cannot print from ASP.NET pages or Windows services using .NET. 根据此页面,您无法使用.NET从ASP.NET页面或Windows服务进行打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM