c# - 从 Windows 服务打印 pcl 文件的 LPR 命令不起作用(现在是托盘应用程序)

标签 c# printing windows-services processstartinfo lpr

我一直在四处寻找可能的解决方案和解释,但我真的找不到任何东西。

正在从 Windows 服务运行以下命令。如果直接在 cmd 中使用,则相同的命令会起作用。 它不会返回任何错误或与此相关的任何其他信息。

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "/C lpr.exe –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + @"\" + fInfo.Name;
    process.StartInfo = startInfo;
    process.Start();

这可能只是我遗漏的一些小东西,但我就是看不到它。如果可以使用 lpr-command 和一个简单的替代方案,我会很高兴,但我还没有看到任何东西。

编辑: 忘了补充一点,我要发送到打印机的文件是一个 pcl 文件。

编辑2: 当我在没有将 Hidden windowstyle 和 WaitForExit(5000) 应用于进程的情况下运行命令时,我似乎看不到任何命令行写入 - 出现的只是空命令提示符。

编辑 3: 现在一直在玩弄这个,我想出了以下内容:

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "lpr";
    startInfo.Arguments = " –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + @"\" + fInfo.Name;
    process.StartInfo = startInfo;
    process.Start();

如果用户单击表单中的按钮执行上述代码,则该代码有效。所以我决定将我的代码更改为作为托盘应用程序运行,因为我认为这可能会解决问题 - 但它似乎仍然拒绝运行。它是由触发的计时器或另一个线程运行的某种问题吗?或者可能与这些方法的权利有关?

最佳答案

将您的代码更改为:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C C:\windows\Sysnative\lpr.exe –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + @"\" + fInfo.Name;
process.StartInfo = startInfo;
process.Start();

问题是您正在尝试从 32 位 cmd.exe 应用程序访问 64 位应用程序 (lpr)。简单的解决方案是使用 sysnative 目录。

http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm

The 'Sysnative' folder is invisible in Windows Explorer If you start Windows Explorer and open the Windows folder on your hard disk, you may notice that the Sysnative folder is not shown. The main reason to this is that Windows Explorer is a 64-bit program (when run in a 64-bit Windows), and the Sysnative folder is only visible and accessible from 32-bit software. If 64-bit software need access to the 64-bit system folder in Windows, the only option is to use the System32 folder name (for example: C:\Windows\System32).

Using the 'Sysnative' folder will help you access 64-bit tools from 32-bit code Some tools in a 64-bit Windows only exist in a 64-bit version; there is no 32-bit version available. And some of these tools are located in the 64-bit System32 folder. One example is the nbtstat tool that is used to help troubleshoot NetBIOS name resolution problems. If you try to run the nbtstat tool from 32-bit code (for example from an application or from script) and use a path like C:\Windows\System32, you will get a "File not found" error. The file can not be found; although Windows Explorer shows that the nbtstat program file actually is located in the C:\Windows\System32 folder.
The solution to this (somewhat confusing) problem is to include the virtual Sysnative folder in the folder path when you want to run the tool. For example like this: C:\Windows\Sysnative\nbtstat.exe The file path above will give you access to the 64-bit nbtstat tool from a 32-bit application or from a 32-bit script. We recommend you to read this article / blog post (at Scottie’s Tech.Info) to get more details about this.

关于c# - 从 Windows 服务打印 pcl 文件的 LPR 命令不起作用(现在是托盘应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11915241/

相关文章:

c# - 使用codedui进行跨浏览器测试?

javascript - 从 Xamarin Forms 中的 WebView 获取数据

java - 如何制作一个接受文件名并可以打印其内容的类?

windows - 用于重新启动 Windows 服务的桌面快捷方式

c# - 在 protobuf-net v3 更新中替换 IList<>?

c# - 如何提高 OCR 识别率?

java - 按钮操作延迟或退出后执行

java - 打印数组/反转数组

c# - 如何在远程服务器上运行命令行程序

c# - TopShelf 服务在异常时卡在 'Stopping' 状态