public class Posprinter {const int open_existing = 3; String prnport = "LPT1"; [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] private static extern IntPtr CreateFile (String lpfilename, int dwdesiredaccess, int dwsharemode, int lpsecurityattributes, int dwcreationdisposition, int dwflagsandattributes, int htemplatefile); Public Posprinter () {} public Posprinter (string prnport) {This.prnport = prnport;//Printer Port} public string PrintLine (String str) {IntPtr ihandle = CreateFile (prnpor T, 0x50000000, 0, 0, open_existing, 0, 0); if (ihandle.toint32 () = =-1) {Console.WriteLine (ihandle.tostring ()); Return "Not connected to the printer or the printer port is not LPT1"; } else {Console.WriteLine (ihandle.tostring ()); FileStream fs = new FileStream (Ihandle, FIleaccess.readwrite); StreamWriter SW = new StreamWriter (FS, System.Text.Encoding.Default); Sw. WriteLine ("Small ticket list"); Sw. WriteLine (); Sw. WriteLine (str); Sw. WriteLine ("Print content"); Sw. WriteLine ("---------------------------"); Sw. Close (); Fs. Close (); Return "Print succeeded!"; } } }
Direct call to PrintLine () method to print the specific required parameters and printing format for everyone to adjust themselves.
C # Backend Call LPT1 port implementation of small ticket machine printing method.