DOS と同様に、次のことができます。
ECHO MESSAGE>LPT1
C# .NET で同じことを実現するにはどうすればよいでしょうか?
C# .NET を使用すると、COM1 に情報を送信するのは簡単なようです。
LPT1 ポートはどうですか?
Escape コマンドをサーマル プリンターに送信したい。
C# 4.0 以降では、最初にCreateFile
メソッドを使用してそのポートに接続し、次にそのポートへのファイルストリームを開いて最終的に書き込みを行う必要があります。上のプリンターに 2 行を書き込むサンプル クラスを次に示します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;
}
}
}
プリンターがLPT1
ポートに接続されていると仮定すると、そうでない場合は、CreateFile
使用しているポートに合わせて方法を調整する必要があります。
次の行を使用して、プログラムのどこでもメソッドを呼び出すことができます
Print2LPT.Print();
これがあなたの問題に対する最短かつ最も効率的な解決策だと思います。