2011-01-14 9 views

答えて

1

いつでもこのコードexampleを試すことができます。

Brで アンダース

0

は、Microsoftからこの記事からいくつかの助けを得る必要があります:C#4.0以降その可能で How to send raw data to a printer by using Visual C# .NET

+0

これはあまりにも複雑です。私はライブラリとライブラリを使わずに直接LPTに通信したいので...私が例で言及したように、それは可能ではありませんか? – HackTweaks

2

を、最初にあなたはCreateFileメソッドを使用して、そのポートに接続する必要があり、その後オープンそのポートに最終的に書き込むためのファイルストリーム。 LPT1に2行をプリンタに書き込むサンプルクラスを次に示します。

あなたが使用しているポートと一致する CreateFile方法を調整する必要がありますされていない場合、お使いのプリンタは、 LPT1ポートに接続されていると仮定すると、
using Microsoft.Win32.SafeHandles; 
using System; 
using System.IO; 
using System.Runtime.InteropServices; 

namespace YourNamespace 
{ 
    public static class Print2LPT 
     { 
      [DllImport("kernel32.dll", SetLastError = true)] 
      static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess,uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); 

      public static bool Print() 
      { 
       string nl = Convert.ToChar(13).ToString() + Convert.ToChar(10).ToString(); 
       bool IsConnected= false; 

       string sampleText ="Hello World!" + nl + 
       "Enjoy Printing...";  
       try 
       { 
        Byte[] buffer = new byte[sampleText.Length]; 
        buffer = System.Text.Encoding.ASCII.GetBytes(sampleText); 

        SafeFileHandle fh = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero); 
        if (!fh.IsInvalid) 
        { 
         IsConnected= true;      
         FileStream lpt1 = new FileStream(fh,FileAccess.ReadWrite); 
         lpt1.Write(buffer, 0, buffer.Length); 
         lpt1.Close(); 
        } 

       } 
       catch (Exception ex) 
       { 
        string message = ex.Message; 
       } 

       return IsConnected; 
      } 
     } 
} 

私はこれがあなたの問題への最短かつ最も効率的なソリューションであると考え、次の行

Print2LPT.Print(); 

でどこでもあなたのプログラム内のメソッドを呼び出すことができます。

関連する問題