在vb中可以直接 open "LPT1:" "string"
在c#中难道只能调用api?

解决方案 »

  1.   

    reference:
    http://www.codeproject.com/useritems/LPrintWriter.asp
      

  2.   

    多谢!!!
    但是好像还是没能解决,再csdn上搜索lpt1的也出来很多类似的提问,但好像都没有彻底解决的反感
      

  3.   

    难道没有人知道么?
    上次有人给我的只有这个调用API的方法...不知道能不能行
     ///////////////调用windows API封装成LPTControl类/////////////////
            public class LPTControl
            {
                [StructLayout(LayoutKind.Sequential)]
                private struct OVERLAPPED
                {
                    int Internal;
                    int InternalHigh;
                    int Offset;
                    int OffSetHigh;
                    int hEvent;
                }            [DllImport("kernel32.dll")]
                private static extern int CreateFile(
                string lpFileName,
                uint dwDesiredAccess,
                int dwShareMode,
                int lpSecurityAttributes,
                int dwCreationDisposition,
                int dwFlagsAndAttributes,
                int hTemplateFile
                );
                [DllImport("kernel32.dll")]
                private static extern bool WriteFile(
                int hFile,
                byte[] lpBuffer,
                int nNumberOfBytesToWrite,
                out int lpNumberOfBytesWritten,
                out OVERLAPPED lpOverlapped
                );
                [DllImport("kernel32.dll")]
                private static extern bool CloseHandle(
                int hObject
                );            private int iHandle;
                public bool Open()
                {
                    iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
                    if (iHandle != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }            }
                public bool Write(String Mystring)
                {
                    if (iHandle != -1)
                    {
                        int i;
                        OVERLAPPED x;
                        byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                        return WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
                    }
                    else
                    {
                        throw new Exception("端口未打开!");
                    }
                }
                public bool Close()
                {
                    return CloseHandle(iHandle);
                }
            }
      

  4.   

    用上面的类好象并不能工作,是不是需要首先驱动打印机?
    但是在cmd下直接用copy都可以把string传给打印机让其工作啊....是不是上面的类里面对于打印机的设置有不对的地方?