print pdf file to network printer using printer IP address
-
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(); } } }
-
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(); } } }
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 = &h20arrComputers = 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
Nextjust 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 :)
-
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 = &h20arrComputers = 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
Nextjust 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 :)
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.
-
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.
have a look at this http://support.microsoft.com/kb/260142 it may help good luck