直接发送文本文件到网络打印机 [英] Send text file directly to network printer

查看:697
本文介绍了直接发送文本文件到网络打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的这写一个临时文件发送原始数据到打印机的代码,然后用 File.Copy()将其发送到打印机。 File.Copy()支持本地端口,如 LPT1 键,如 \共享打印机\FRONTCOUNTER\LabelPrinter

I have currently-working code which sends raw data to a printer by writing a temporary file, then using File.Copy() to send it to the printer. File.Copy() supports both local ports, like LPT1 and shared printers like \\FRONTCOUNTER\LabelPrinter.

不过,现在我试图把它与那直接在网络上的打印机工作: 192.168.2.100 ,我想不出要使用的格式。

However, now I'm trying to get it working with a printer that's directly on the network: 192.168.2.100, and I can't figure out the format to use.

File.Copy(filename, @"LPT1", true); // Works, on the FRONTCOUNTER computer
File.Copy(filename, @"\\FRONTCOUNTER\LabelPrinter", true); // Works from any computer
File.Copy(filename, @"\\192.168.2.100", true); // New printer, Does not work



我知道这是可以从每台计算机的添加打印机 ,但我希望避免 - 自动第二码以上作品在网络上的线路从任何计算机,无需配置。我也知道这是可能的P / Invoke Windows打印后台处理程序,如果这是我唯一的选择,我是否可以,但是这更多的代码开销比我想有。

I know it's possible to "Add a printer" from each computer, but I'm hoping to avoid that - the second line of code above works from any computer on the network automatically, with no configuration required. I also know it's possible to P/Invoke the windows print spooler, and if that's my only option I may take it, but that's much more code overhead than I'd like to have.

在理想情况下,会有人要么办法让 File.Copy()工作或类似的C#声明,将接受网络IP。

Ideally, someone will have either a way to make File.Copy() work or a similar C# statement which will accept a network IP.

推荐答案

您可以使用套接字和数据直接发送到该IP地址。应该几乎是相同的 File.Copy 。我只是想出来,那工作。

You can use sockets and send the data straight to that IP address. Should pretty much be the same as File.Copy. I just tried it out and that worked.

我刚刚给一些文字,但这里是我用

I just sent some text but here is the code that I used

Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;

IPAddress ip = IPAddress.Parse("192.168.192.6");
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);

byte[] fileBytes = File.ReadAllBytes("test.txt");

clientSocket.Send(fileBytes);
clientSocket.Close();

这篇关于直接发送文本文件到网络打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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