C#使用控制台列出当前所有可用的打印机列表

C#使用控制台列出当前所有可用打印机列表的方法。分享给大家供大家参考。具体如下:

// The initial C# code for the WMI query was generated by WMI Code //Generator, Version 5.00, http://www.robvanderwoude.com/wmigen.php using System; using System.Management; using System.Collections; namespace RobvanderWoude {  public class ListPrinters  {   public static int Main( string[] args )   {    try   {     string computer = string.Empty;     #region Command line parsing     // Only 1 optional argument allowed: a remote computer name     if ( args.Length > 1 )     {      throw new Exception( "Invalid command line arguments" );     }     if ( args.Length == 1 )     {      // We‘ll display a ‘friendly‘ message if help was requested      if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )      {       switch ( args[0].ToUpper( ) )       {        case "/?":        case "-?":        case "/H":        case "-H":        case "--H":        case "/HELP":        case "-HELP":        case "--HELP":         return WriteError( string.Empty );        default:         return WriteError( "Invalid command line argument" );       }      }      else     {       computer = "\\\\" + args[0] + "\\";      }     }     #endregion     string wmins = computer + "root\\CIMV2";     ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmins, "SELECT * FROM Win32_Printer" );     ArrayList printers = new ArrayList( );     foreach ( ManagementObject queryObj in searcher.Get( ) )     {      printers.Add( queryObj["DeviceID"] );     }     printers.Sort( );     foreach ( string printer in printers )     {      Console.WriteLine( printer );     }     return 0;    }    catch ( Exception e )    {     return WriteError( e );    }   }   public static int WriteError( Exception e )   {    return WriteError( e == null ? null : e.Message );   }   public static int WriteError( string errorMessage )   {    string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );    string[] program = fullpath.Split( ‘\\‘ );    string exename = program[program.GetUpperBound( 0 )];    exename = exename.Substring( 0, exename.IndexOf( ‘.‘ ) );    if ( string.IsNullOrEmpty( errorMessage ) == false )    {     Console.Error.WriteLine( );     Console.ForegroundColor = ConsoleColor.Red;     Console.Error.Write( "ERROR: " );     Console.ForegroundColor = ConsoleColor.White;     Console.Error.WriteLine( errorMessage );     Console.ResetColor( );    }    Console.Error.WriteLine( );    Console.Error.WriteLine( exename + ", Version 1.10" );    Console.Error.WriteLine( "List all local printers on the specified computer" );    Console.Error.WriteLine( );    Console.Error.Write( "Usage: " );    Console.ForegroundColor = ConsoleColor.White;    Console.Error.Write( exename.ToUpper( ) );    Console.Error.WriteLine( " [ computername ]" );    Console.ResetColor( );    Console.Error.WriteLine( );    Console.Error.WriteLine( "Where: ‘computername‘ is the (optional) name of a remote computer" );    Console.Error.WriteLine( " (default if not specified: local computer)" );    Console.Error.WriteLine( );    Console.Error.WriteLine( "Written by Rob van der Woude" );    return 1;   }  } }

codego.net代码

时间: 2024-10-24 00:27:11

C#使用控制台列出当前所有可用的打印机列表的相关文章

C# 获取打印机列表以及串口

C# 获取打印机列表以及默认打印机.串口列表. /// <summary> /// 获取本地已安装的打印机 /// </summary> /// <returns></returns> public string GetPrinter() { string strList = ""; System.Drawing.Printing.PrinterSettings.StringCollection PrinterList = System.

C#获得本地打印机列表的办法

public class LocalPrinter { private static PrintDocument fPrintDocument = new PrintDocument(); /// <summary> /// 获取本机默认打印机名称 /// </summary> public static String DefaultPrinter { get { return fPrintDocument.PrinterSettings.PrinterName; } } ///

[C#] 获取打印机列表

一:获得本地安装的打印机列表 注:(如果在"设备和打印机"中已经添加了局域网的打印机设备,也算是本地安装的打印机:没有添加的则算作局域网打印机) 1,通过C#中PrinterSettings对象获取,如下,然后通过foreach即可遍历printers: PrinterSettings.StringCollection printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters; 2,PrintHelper类

C#获取本地打印机列表,并将指定打印机设置为默认打印机

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Med

从注册表中删除打印机列表中的打印机

删除网络打印机:从注册表项中HKEY_CURRENT_USER/Printers/Connections 下找到要删除的打印机项删除即可 删除本地打印机HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers 下找到要删除的打印机项删除即可 HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers下找到要删除的打印机项删除即可 最后重启打印机服

python扫描proxy并获取可用代理ip列表

mac或linux下可以work的代码如下: # coding=utf-8 import requests import re from bs4 import BeautifulSoup as bs import Queue import threading import random import re headers_useragents = [] headers_referers = [] headers_referers.append('http://www.google.com/?q=

获取WINDOWS打印机列表

如何知道WINDOWS已经安装了哪些打印机? 1) usesVcl.Printers 2) Printer.Printers  // property Printers: TStrings read GetPrinters; 3) 原文地址:https://www.cnblogs.com/hnxxcxg/p/9916906.html

收集公网可用的FTP列表

ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ ftp://[email protected]/ 未完待续,发现新的后会第一时间补上,本列表可用于测

C#调用windows api 实现打印机控制

using System; using System.Text; using System.Runtime.InteropServices; using System.Security; using System.ComponentModel; namespace SoftPOS { public class PrinterHelper { private PrinterHelper(){ } #region API声明 [StructLayout(LayoutKind.Sequential,