Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. print pdf file to network printer using printer IP address

print pdf file to network printer using printer IP address

Scheduled Pinned Locked Moved C#
csharpsysadminhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    There is always the way to do it but I dont know
    wrote on last edited by
    #1

    I use following code in C# to print pdf files, it works ok, I want to use printer's IP address instead of printer's name, any help? private static void doPrint(string executable, string fileFullPath, string printerName) { // Print PDF file using Acrobat Reader. // "/h" - hidden mode // "/t" - print command following by the file name and printer name System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = "/h /t \"" + fileFullPath + "\" \"" + printerName + "\""; startInfo.FileName = executable; startInfo.UseShellExecute = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; System.Diagnostics .Process process = new Process() ; try { process = Process.Start(startInfo); } catch (Exception ex) { DBMaint.updateLogFile (ex.Message.ToString ()); } finally { process.WaitForExit(20000); if (process.HasExited == false) { process.Kill(); } } }

    H 1 Reply Last reply
    0
    • T There is always the way to do it but I dont know

      I use following code in C# to print pdf files, it works ok, I want to use printer's IP address instead of printer's name, any help? private static void doPrint(string executable, string fileFullPath, string printerName) { // Print PDF file using Acrobat Reader. // "/h" - hidden mode // "/t" - print command following by the file name and printer name System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = "/h /t \"" + fileFullPath + "\" \"" + printerName + "\""; startInfo.FileName = executable; startInfo.UseShellExecute = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; System.Diagnostics .Process process = new Process() ; try { process = Process.Start(startInfo); } catch (Exception ex) { DBMaint.updateLogFile (ex.Message.ToString ()); } finally { process.WaitForExit(20000); if (process.HasExited == false) { process.Kill(); } } }

      H Offline
      H Offline
      Hessam Jalali
      wrote on last edited by
      #2

      Each printer has a port for accessing that if you put the port name instead the printer name it would work. ports for network printers define like this \\Hostname\portName [somewhat like printerName but not exactly] and you can put IP of the host instead of the name. so you should do 2 things first find the printer port and second find the IP of the host the first one need some works to do, one of the ways is to use WMI through System.Managment namespace in .NET and here is the script you need for retrieving printers information

      On Error Resume Next

      Const wbemFlagReturnImmediately = &h10
      Const wbemFlagForwardOnly = &h20

      arrComputers = Array("TITAN")
      For Each strComputer In arrComputers
      WScript.Echo
      WScript.Echo "=========================================="
      WScript.Echo "Computer: " & strComputer
      WScript.Echo "=========================================="

      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PrinterShare", "WQL", _
      wbemFlagReturnImmediately + wbemFlagForwardOnly)

      For Each objItem In colItems
      WScript.Echo "Antecedent: " & objItem.Antecedent
      WScript.Echo "Dependent: " & objItem.Dependent
      WScript.Echo
      Next
      Next

      just use the query part to create and use it Here is the code for showing the Printer ports

              ManagementObjectSearcher search = new ManagementObjectSearcher(new SelectQuery("Win32\_Printer"));
              ManagementObjectCollection moc = search.Get();
      
              foreach (ManagementObject mo in moc)
                  MessageBox.Show(mo.Properties\["Name"\].Value.ToString()+"   Port:   "+mo.Properties\["PortName"\].Value.ToString());
      

      and the second part is fairly easy just use the Dns.GetHostEntry method to get the IP and at last I test your code with this method and it works hope the post would be useful :)

      T 1 Reply Last reply
      0
      • H Hessam Jalali

        Each printer has a port for accessing that if you put the port name instead the printer name it would work. ports for network printers define like this \\Hostname\portName [somewhat like printerName but not exactly] and you can put IP of the host instead of the name. so you should do 2 things first find the printer port and second find the IP of the host the first one need some works to do, one of the ways is to use WMI through System.Managment namespace in .NET and here is the script you need for retrieving printers information

        On Error Resume Next

        Const wbemFlagReturnImmediately = &h10
        Const wbemFlagForwardOnly = &h20

        arrComputers = Array("TITAN")
        For Each strComputer In arrComputers
        WScript.Echo
        WScript.Echo "=========================================="
        WScript.Echo "Computer: " & strComputer
        WScript.Echo "=========================================="

        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
        Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PrinterShare", "WQL", _
        wbemFlagReturnImmediately + wbemFlagForwardOnly)

        For Each objItem In colItems
        WScript.Echo "Antecedent: " & objItem.Antecedent
        WScript.Echo "Dependent: " & objItem.Dependent
        WScript.Echo
        Next
        Next

        just use the query part to create and use it Here is the code for showing the Printer ports

                ManagementObjectSearcher search = new ManagementObjectSearcher(new SelectQuery("Win32\_Printer"));
                ManagementObjectCollection moc = search.Get();
        
                foreach (ManagementObject mo in moc)
                    MessageBox.Show(mo.Properties\["Name"\].Value.ToString()+"   Port:   "+mo.Properties\["PortName"\].Value.ToString());
        

        and the second part is fairly easy just use the Dns.GetHostEntry method to get the IP and at last I test your code with this method and it works hope the post would be useful :)

        T Offline
        T Offline
        There is always the way to do it but I dont know
        wrote on last edited by
        #3

        Thanks for the very usful information, it works for my project. But I had little problem also, when I run it from my computer, it's ok, when I deployeed to another computer and scheduled in windows task, I got error,debug window pop up, in windows task log file, it exited with code C000005. I couldn't figure out what cause the error. Any help again?? Thanks a lot.

        H 1 Reply Last reply
        0
        • T There is always the way to do it but I dont know

          Thanks for the very usful information, it works for my project. But I had little problem also, when I run it from my computer, it's ok, when I deployeed to another computer and scheduled in windows task, I got error,debug window pop up, in windows task log file, it exited with code C000005. I couldn't figure out what cause the error. Any help again?? Thanks a lot.

          H Offline
          H Offline
          Hessam Jalali
          wrote on last edited by
          #4

          have a look at this http://support.microsoft.com/kb/260142 it may help good luck

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups