我正在尝试连接打印机,但状态总是显示错误。如何编程连接打印机。
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;
}
}
}
这是一个很好的例子。
欢迎来到c#打印教程
在过去,用微软语言打印文档很容易。有了像Visual Basic这样的语言,它就是一台简单的打印机。打印的电话。在那些日子里,在那种语言中,我们会调用它,文档就会打印出来,但由于那不是面向对象的语言,我们无法控制任何东西。
快进到神奇的2000年,微软在这一年推出了c#。有了c#,就不再有Printer了。用于打印文档的打印解决方案。由于c#是一种面向对象的语言,你现在可以控制整个打印过程,从字体到字体大小,到页面的方向(横向,纵向),再到打印区域的大小(页面大小),这当然需要程序员做一些工作。这就是本教程的目的,也是我每天至少问一次如何完成这个任务的事实。