使用IP地址打印到网络打印机

时间:2014-09-13 12:25:44

标签: c# .net winforms

我想发送文件名和打印机的IP地址来指定要打印的打印机。

我收到错误消息“访问打印机的设置'xxx.xxx.xxx.xxx'无效。”当我到printdoc.Print()。

如何根据IP地址设置要打印的打印机?

    printdoc = new PrintDocument();
    printdoc.PrinterSettings.PrinterName = IPAddress.Trim;
    printdoc.DocumentName = FileName;
    printdoc.Print();

如何解决这个问题?它是一个c#vs2010独立Windows应用程序

4 个答案:

答案 0 :(得分:1)

您不能使用IPAddress。打印机必须已经安装在计算机上。

在某些系统上,该功能仅供管理员使用,因此您的应用程序不应创建打印机。毕竟,您也没有每种打印机类型的驱动程序。

您的应用只能获取要使用的已安装打印机的名称。您不能仅使用IP地址。

答案 1 :(得分:1)

我终于能够做到这一点

VB: 昏暗

Imports System.IO
Imports System.Net, System.Net.Sockets
.
.
.
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
  Try
    Dim IPAddress As String = txtIPAddr.Text 'ie: 10.0.0.91;
    Dim port As Integer = txtPort.Text 'ie: 9100

    Dim client As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient()
    client.Connect(IPAddress, port)
    Dim reader As StreamReader = New StreamReader(txtFilename.Text) 'ie: C:\\Apps\\test.txt
    Dim writer As StreamWriter = New StreamWriter(client.GetStream())
    Dim testFile As String = reader.ReadToEnd()
    reader.Close()
    writer.Write(testFile)
    writer.WriteLine("Hello World!")
    writer.Flush()
    writer.Close()
    client.Close()
  Catch ex As Exception
    MessageBox.Show("Error: " + ex.Message)
  End Try
End Sub

C#:

using System.IO;
using System.Net;
using System.Net.Sockets;
.
.
.
private void btnPrint_Click(object sender, EventArgs e) {
  try {
    string ipAddress = txtIPAddr.Text.ToString(); ; //ie: 10.0.0.91
    int port = int.Parse(txtPort.Text.ToString()); //ie: 9100

    System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
    client.Connect(ipAddress, port);
    StreamReader reader = new StreamReader(txtFilename.Text.ToString()); //ie: C:\\Apps\\test.txt
    StreamWriter writer = new StreamWriter(client.GetStream());
    string testFile = reader.ReadToEnd();
    reader.Close();
    writer.Write(testFile);
    writer.WriteLine("Hello World!");
    writer.Flush();
    writer.Close();
    client.Close();
  }
  catch (Exception ex) {
    MessageBox.Show(ex.Message, "Error");
  }
}

答案 2 :(得分:0)

您的打印机是否可以通过网络访问您正在运行软件的计算机?

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.printername.aspx

如您所见,您应该调用IsValid来确定一切是否正常,还可以使用InstalledPrinters属性来获取系统上安装的打印机列表。我猜您没有正确安装打印机,或者您没有足够的权限或类似的东西。

编辑:如果使用名称有效,这应该可以解决问题:How to access a printer name from IP on network in C#?

答案 3 :(得分:0)

这是IP打印机的完整工作代码(型号GK420t ZPL,您可以访问任何IP打印机)。只需替换三件事 1)添加您的IP地址 2)添加您的端口号 3)添加您的PNG文件路径

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Http;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Printing;
using System.Net.NetworkInformation;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Printing;





namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {     
            string ipAddress = "Your IP address";
            int port = Your port number;

            string zplImageData = string.Empty;
            string filePath = @"your png file path";
            byte[] binaryData = System.IO.File.ReadAllBytes(filePath);
            foreach (Byte b in binaryData)
            {
                string hexRep = String.Format("{0:X}", b);
                if (hexRep.Length == 1)
                    hexRep = "0" + hexRep;
                zplImageData += hexRep;
            }
            string zplToSend = "^XA" + "^FO50" + "50^GFA,120000,120000,100" + binaryData.Length + ",," + zplImageData + "^XZ";
            string printImage = "^XA^FO115,50^IME:LOGO.PNG^FS^XZ";

            try
            {
                // Open connection
                System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
                client.Connect(ipAddress, port);

                // Write ZPL String to connection
                System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream(), Encoding.UTF8);
                writer.Write(zplToSend);
                writer.Flush();
                writer.Write(printImage);
                writer.Flush();
                // Close Connection
                writer.Close();
                client.Close();
            }
            catch (Exception ex)
            {
                // Catch Exception
            }


    }
        }

    }

enter image description here