I have a program that can send print command to LPT source code below which i also followed from Send print commands directly to LPT parallel port using C#.net
Is there a way to check connectivity of LPT port programatically? Like if its online or connected to the computer?
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public bool Print(string Command)
{
string nl = Convert.ToChar(13).ToString() + Convert.ToChar(10).ToString();
bool IsConnected = false;
try
{
log.Info("COMMAND TO PRINT" + Command);
Byte[] buffer = new byte[Command.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(Command);
FileStream lpt1 = null;
SafeFileHandle fh = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
if (!fh.IsInvalid)
{
IsConnected = true;
lpt1 = new FileStream(fh, FileAccess.ReadWrite);
lpt1.Write(buffer, 0, buffer.Length);
lpt1.Close();
}
lpt1.Dispose();
if (lpt1 == null)
{
log.Info("LPT File is Disposed");
}
fh.Dispose();
}
catch (Exception ex)
{
string message = ex.Message;
throw ex;
}
log.InfoFormat("Connected: " + IsConnected);
return IsConnected;
}