正在阅读:Delphi必学技巧:枚举打印机Delphi必学技巧:枚举打印机

2004-12-15 16:51 出处:CSDN 作者:apexchu 责任编辑:linjixiong

  在某些情况下,我们需要枚举网内的打印机,试试以下的代码,很有可能帮得了你。



  using System;
  using System.Runtime.InteropServices;
  using System.Runtime.Serialization;

  namespace Sample.GetPrinter
  {
  class App
  {
  static void Main(string[] args)
  {
  Console.WriteLine("请输入你想搜索类型的序号:");
  Console.WriteLine("1. Default printer(only Win95,Win98,WinME)\n2. Enumerates the locally installed printers;\n3. Enumerates the list of printers to which the user has made previous connections;\n4. Enumerates the printer identified by Name.;\n5. Enumerates network printers and print servers in the computer's domain;\n6. Enumerates printers that have the shared attribute;\n7. Enumerates network printers in the computer's domain;\n==========================");
  
  int pt =0;
  try
  {
  pt=Int32.Parse(Console.ReadLine());
  }
  catch(Exception e)
  {
  Console.WriteLine("错误信息: {0}",e.Message);
  return;
  }

  PRINTER_ENUM printerKind = GetPrinterConType(pt);

  PrinterSearcher p = new PrinterSearcher(); 

  PrinterSearcher.PrinterInfo[] printers = p.Search(printerKind);

  foreach(PrinterSearcher.PrinterInfo pi in printers)
  {
  Console.WriteLine("=====================================\n打印机名: {0}\n描叙:: {1}\n注释: {2}\n=====================================\n",
  pi.Name, pi.Description, pi.Comment);
  }
  }

  static PRINTER_ENUM GetPrinterConType(int ins)
  {
  switch(ins)
  {
  case 1:
  return PRINTER_ENUM.DEFAULT ;
  case 2:
  return PRINTER_ENUM.LOCAL;
  case 3:
  return PRINTER_ENUM.CONNECTIONS;
  case 4:
  return PRINTER_ENUM.NAME;
  case 5:
  return PRINTER_ENUM.REMOTE;


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:

键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品