Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listprinters.cs

(view source code of listprinters.cs as plain text)

  1. // The initial C# code for the WMI query was generated by WMI Code Generator, Version 5.00, http://www.robvanderwoude.com/wmigen.php
  2.  
  3. using System;
  4. using System.Management;
  5. using System.Collections;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	public class ListPrinters
  11. 	{
  12. 		public static int Main( string[] args )
  13. 		{
  14. 			try
  15. 			{
  16. 				string computer = string.Empty;
  17.  
  18. 				#region Command line parsing
  19.  
  20. 				// Only 1 optional argument allowed: a remote computer name
  21. 				if ( args.Length > 1 )
  22. 				{
  23. 					throw new Exception( "Invalid command line arguments" );
  24. 				}
  25. 				if ( args.Length == 1 )
  26. 				{
  27. 					// We'll display a 'friendly' message if help was requested
  28. 					if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) )
  29. 					{
  30. 						switch ( args[0].ToUpper( ) )
  31. 						{
  32. 							case "/?":
  33. 							case "-?":
  34. 							case "/H":
  35. 							case "-H":
  36. 							case "--H":
  37. 							case "/HELP":
  38. 							case "-HELP":
  39. 							case "--HELP":
  40. 								return WriteError( string.Empty );
  41. 							default:
  42. 								return WriteError( "Invalid command line argument" );
  43. 						}
  44. 					}
  45. 					else
  46. 					{
  47. 						computer = "\\\\" + args[0] + "\\";
  48. 					}
  49. 				}
  50.  
  51. 				#endregion
  52.  
  53. 				string wmins = computer + "root\\CIMV2";
  54.  
  55. 				ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmins, "SELECT * FROM Win32_Printer" );
  56.  
  57. 				ArrayList printers = new ArrayList( );
  58.  
  59. 				foreach ( ManagementObject queryObj in searcher.Get( ) )
  60. 				{
  61. 					printers.Add( queryObj["DeviceID"] );
  62. 				}
  63.  
  64. 				printers.Sort( );
  65.  
  66. 				foreach ( string printer in printers )
  67. 				{
  68. 					Console.WriteLine( printer );
  69. 				}
  70.  
  71. 				return 0;
  72. 			}
  73. 			catch ( Exception e )
  74. 			{
  75. 				return WriteError( e );
  76. 			}
  77. 		}
  78.  
  79. 		public static int WriteError( Exception e )
  80. 		{
  81. 			return WriteError( e == null ? null : e.Message );
  82. 		}
  83.  
  84. 		public static int WriteError( string errorMessage )
  85. 		{
  86. 			/*
  87. 			ListPrinters,  Version 1.10
  88. 			List all local printers on the specified computer
  89.  
  90. 			Usage:  LISTPRINTERS  [ computername ]
  91.  
  92. 			Where:  'computername'  is the (optional) name of a remote computer
  93. 									(default if not specified: local computer)
  94.  
  95. 			Written by Rob van der Woude
  96. 			http://www.robvanderwoude.com
  97. 			*/
  98.  
  99. 			string fullpath = Environment.GetCommandLineArgs( ).GetValue( 0 ).ToString( );
  100. 			string[] program = fullpath.Split( '\\' );
  101. 			string exename = program[program.GetUpperBound( 0 )];
  102. 			exename = exename.Substring( 0, exename.IndexOf( '.' ) );
  103.  
  104. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  105. 			{
  106. 				Console.Error.WriteLine( );
  107. 				Console.ForegroundColor = ConsoleColor.Red;
  108. 				Console.Error.Write( "ERROR:  " );
  109. 				Console.ForegroundColor = ConsoleColor.White;
  110. 				Console.Error.WriteLine( errorMessage );
  111. 				Console.ResetColor( );
  112. 			}
  113. 			Console.Error.WriteLine( );
  114. 			Console.Error.WriteLine( exename + ",  Version 1.10" );
  115. 			Console.Error.WriteLine( "List all local printers on the specified computer" );
  116. 			Console.Error.WriteLine( );
  117. 			Console.Error.Write( "Usage:  " );
  118. 			Console.ForegroundColor = ConsoleColor.White;
  119. 			Console.Error.Write( exename.ToUpper( ) );
  120. 			Console.Error.WriteLine( "  [ computername ]" );
  121. 			Console.ResetColor( );
  122. 			Console.Error.WriteLine( );
  123. 			Console.Error.WriteLine( "Where:  'computername'  is the (optional) name of a remote computer" );
  124. 			Console.Error.WriteLine( "                        (default if not specified: local computer)" );
  125. 			Console.Error.WriteLine( );
  126. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  127. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  128. 			return 1;
  129. 		}
  130. 	}
  131. }
  132.  

page last modified: 2024-04-16; loaded in 0.0221 seconds