我尝试连接打印机,但状态始终显示错误。如何编码连接打印机。
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;
}
}
}
答案 0 :(得分:0)
这是一个很好的例子。
Welcome to this tutorial on Printing in C#
过去,用Microsoft语言打印文档很容易,并且在白天回来了。使用Visual Basic等语言,这是一个简单的Printer.Print调用。在那些日子里,用那种语言,我们会称之为文档并打印,但由于那不是面向对象的语言,我们无法控制任何东西。
快到多年,到2000年神奇的一年,微软推出了C#。使用C#,不再有用于打印文档的Printer.Print解决方案。由于C#是面向对象的语言,您现在可以控制整个打印过程,从字体到字体大小,到页面方向(横向,纵向),到打印区域的大小(页面)大小),这当然需要程序员的一些工作部分。这就是本教程的目的,以及我每天至少问过一次如何完成这项任务的事实。