Reputation: 29
I am trying to connect with printer but status always show error. How to code to connect with printer.
namespace CustomFunction
{
public class Custom
{
public Custom();
public string PaperStatus();
public string PrinterModel();
}
}
namespace CustomPrinterStatusCheck
{
public class Program
{
private enum PrinterStatus
{
PaperPresent,
NearPaperEnd, //4
paperAbsent, //5
Error, // typically printer fault or not founds
Default // have not do any check since kiosk is up
}
private static string resultSaveLocation = "";
private static string applicationRunningMode = "";
private static readonly ILog ApplicationExceptionLogger = LogManager.GetLogger("APP.ExceptionLogger");
public static void Main(string[] args)
{
try
{
Bootstrap();
string value = "";
PrinterStatus currentStatus = GetPrinterPaperStatus();
switch (currentStatus)
{
case PrinterStatus.Default:
value = "DFT";
break;
case PrinterStatus.NearPaperEnd:
value = "NPE";
break;
case PrinterStatus.paperAbsent:
value = "PRA";
break;
case PrinterStatus.PaperPresent:
value = "PRP";
break;
case PrinterStatus.Error:
value = "ERR";
break;
}
Console.WriteLine("Printer Status: " + currentStatus.ToString() + " Code:" + value);
WriteResultToTextfile(value);
}
catch (Exception ex)
{
// display the result in console window
Console.WriteLine(ex.Message);
// write the exceptoin / error to log file
ApplicationExceptionLogger.Error(ex);
}
finally
{
// display the result in console window
Console.WriteLine("Operation completed");
// if application is running in text mode, console window will
// not closed by itself
if (applicationRunningMode == "TEST")
{
Console.ReadLine();
}
}
}
/// <summary>
/// clean the file content and write -1 to indicate the failure of validating user
/// </summary>
private static void WriteResultToTextfile(string textToWrite)
{
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + resultSaveLocation;
File.WriteAllText(filePath, textToWrite);
}
/// <summary>
/// setting up the application
/// </summary>
private static void Bootstrap()
{
XmlConfigurator.Configure();
resultSaveLocation = ConfigurationManager.AppSettings["ResultOutputLocation"].ToString();
applicationRunningMode = ConfigurationManager.AppSettings["ApplicationRunningMode"].ToString();
}
/// <summary>
/// function to query custom printer status
/// </summary>
/// <returns></returns>
private static PrinterStatus GetPrinterPaperStatus()
{
PrinterStatus currentPrinterStatus = PrinterStatus.Default;
CustomFunction.Custom printer = new Custom();
string printerStatus = printer.PaperStatus();
if (printerStatus.Contains("0"))
{
currentPrinterStatus = PrinterStatus.PaperPresent;
}
else if (printerStatus.Contains("4"))
{
currentPrinterStatus = PrinterStatus.NearPaperEnd;
}
else if (printerStatus.Contains("5"))
{
currentPrinterStatus = PrinterStatus.paperAbsent;
}
else if (printerStatus.Contains("Error"))
{
currentPrinterStatus = PrinterStatus.Error;
}
return currentPrinterStatus;
}
}
}
Upvotes: 1
Views: 4668
Reputation: 1185
This is good example for this.
Welcome to this tutorial on Printing in C#
It used to be that printing documents in Microsoft languages was easy "back in the day". With languages such as Visual Basic it was a simple Printer.Print call. In those days, in that language, we would call that and the document would print, but since that wasn't an Object Orientated language we had no control over anything.
Fast forward many years to the magical year of 2000, the year Microsoft introduced C#. With C# there was no more Printer.Print solutions for printing your documents. Since C# is an Object Orientated language you are now able to control the entire process of printing, from the font, to the font size, to the orientation of the page (Landscape, Portrait), to the size of the print area (the page size), which of course requires some work on the programmers part. That is the purpose of this tutorial, that and the fact that Im asked at least once a day how to accomplish this task.
Upvotes: 2