0

I want to send data to a printer on LPT1 and i trying exactly this but my CreateFile returns -1 (The system cannot find the file specified.Exception from HRESULT:0x80070002). How to open LPT1 port and send data to? I am trying this on XP and after that in win7 64 bit because from what i've read working with LPT in win7 64 bit is a bit of a problem, or should i say 64 bit of a problem:)

PS:Since it's my first post this year: Happy New year to everybody.

gigi
  • 3,846
  • 7
  • 37
  • 50
  • Parallel ports went the way of the dodo quite a while ago. Does the machine even have one? The exception says it doesn't. Use Device Manager, ask more questions at superuser.com – Hans Passant Jan 02 '11 at 15:38
  • The printer is connected with USB – gigi Jan 02 '11 at 16:09

1 Answers1

2

You can try the following. Works fine for text mode.

'net share' shows the following:

Share name   Resource                        Remark

-------------------------------------------------------------------------------
IPC$                                         Remote IPC
D$           D:\                             Default share
print$       C:\WINDOWS\system32\spool\drivers
                                             Printer Drivers
wwwroot$     c:\inetpub\wwwroot              Used for file share access to web
C$           C:\                             Default share
ADMIN$       C:\WINDOWS                      Remote Admin
SharedDocs   C:\DOCUMENTS AND SETTINGS\ALL USERS\DOCUMENTS

Printer2     IP_192.168.115.227     Spooled  HP LaserJet 2200 Series PS (MS)
TEST         LPT1:                  Spooled  Microsoft XPS Document Writer
The command completed successfully.

And here's the code

using System;
using System.IO;

namespace SimplePrinting
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class SimplePrinting
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string printingTaskFileName = Path.GetTempFileName(); // file in %temp%

            System.IO.FileStream printingTaskFile;
            System.IO.StreamWriter printingTaskStream;

            printingTaskFile = new System.IO.FileStream(printingTaskFileName, FileMode.Append);
            printingTaskStream = new System.IO.StreamWriter(printingTaskFile, System.Text.Encoding.Default);

            printingTaskStream.Write("some content here");
            printingTaskStream.Flush();
            printingTaskStream.Close();

            File.Copy(printingTaskFileName, @"\\127.0.0.1\TEST", true); // also can be \\127.0.0.1\PNT5 or smth like that
            File.Delete(printingTaskFileName);
        }
    }
}
Vladimir Tarasov
  • 631
  • 1
  • 7
  • 10
  • i get : FileNotFound:Could not find file "LPT1" – gigi Jan 02 '11 at 15:17
  • i shared the printer(something i forgot) with the share name "TSCTTP-1" and i tried instead of "LPT1" @"\\127.0.0.1\TSCTTP-1" but i get: IOException:The network location cannot be reached. – gigi Jan 02 '11 at 15:23
  • I'd updated the answer with my settings and the full code fragment. Don't forget to check for user permissions for the shared printer for the user which executes the code. – Vladimir Tarasov Jan 02 '11 at 15:40
  • thanks for your answers, you try to help me and am an idiot, i ran net share in cmd and i have TSCTTP-1......USB001 and i checked in printers and faxes for my printers port and indeed it is set on USB and if i switch to LPT1 it doesn't work, i am talking about, printing through the Bartender software not the one i am trying to create, so i am guessing i have to use USB also in the application i am trying to make right? – gigi Jan 02 '11 at 15:47
  • Well, just access USB printer throw share name. As to me, it's the easiet way. – Vladimir Tarasov Jan 02 '11 at 16:08
  • What should i use to access usb printer through share name? i guess SerialPort is no help – gigi Jan 02 '11 at 16:33