简体   繁体   中英

Print to a network printer using IP address

I want to send in a filename and a printer's IP address to specify which printer to print to.

I am getting an error saying "Settings to access printer 'xxx.xxx.xxx.xxx' are not valid." when I get to printdoc.Print().

How to I set which printer to print to based on the IP Address?

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

How to solve this issue? It's a C# vs2010 standalone Windows application.

I was finally able to get this going

VB: dim

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");
  }
}

Is your printer accessible in network for the machine you're running your software on?

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

As you can see, you should call IsValid to determine if everything is ok and you can also use InstalledPrinters property to get a list of printers installed on the system. I guess you don't have installed the printer correctly or you don't have sufficient permissions or something like that.

edit: if using name works, this should do the trick: How to access a printer name from IP on network in C#?

You can't do using IPAddress. The printer has to be already installed on your Machine.

On some systems, that function is reserved for administrators only so you're app should not be creating printers. After all, you don't have the drivers for every printer type either.

Your app can only get the name of a printer to use that is already installed. You cannot use just the IP address.

here is the complete working code of IP printer ( Model GK420t ZPL And you can access any IP printer ). Just replace only three things 1) Add you IP address 2) add your port number 3) Add you PNG File path

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
            }


    }
        }

    }

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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