Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printing.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Text.RegularExpressions;
  6.  
  7.  
  8.  
  9. namespace RobvanderWoude
  10. {
  11. 	public class Printing
  12. 	{
  13. 		public const string progver = "3.04";
  14.  
  15.  
  16. 		#region Global Variables
  17.  
  18. 		public static char switchchar = '/';
  19. 		public static List<string[]> actionslist = new List<string[]>( );
  20. 		public static List<string[]> printerlist = new List<string[]>( );
  21. 		public static bool actionFlush = false;
  22. 		public static bool actionList = false;
  23. 		public static bool actionPause = false;
  24. 		public static bool actionResume = false;
  25. 		public static string action = String.Empty;
  26.  
  27. 		#endregion Global Variables
  28.  
  29.  
  30. 		public static int Main( string[] args )
  31. 		{
  32. 			try
  33. 			{
  34. 				#region Initialize Variables
  35.  
  36. 				bool actionisset = false;
  37. 				bool takeaction = true;
  38.  
  39. 				bool printerisset = false;
  40. 				bool useAllPrn = false;
  41. 				bool useDefault = false;
  42. 				bool useRegex = false;
  43. 				string pattern = ".*";
  44. 				string printer = String.Empty;
  45.  
  46. 				bool sortisset = false;
  47. 				string sortby = "N"; // sort by printer Name
  48.  
  49. 				WMIPrinterStatus status = WMIPrinterStatus.Unknown;
  50. 				UInt32 jobs = 0;
  51.  
  52. 				int printerscount = 0;
  53. 				int switchcharunix = 0;
  54. 				int switchchardos = 0;
  55.  
  56. 				#endregion Initialize Variables
  57.  
  58. 				#region Command Line Parsing
  59.  
  60. 				if ( args.Length == 0 )
  61. 				{
  62. 					return ErrorMessage( );
  63. 				}
  64. 				if ( args.Length > 4 )
  65. 				{
  66. 					return ErrorMessage( "Too many command line arguments" );
  67. 				}
  68.  
  69. 				foreach ( string arg in args )
  70. 				{
  71. 					if ( arg.Length < 2 )
  72. 					{
  73. 						return ErrorMessage( "Invalid command line argument \"{0}\"", arg );
  74. 					}
  75. 					if ( "-/".Contains( arg[0] ) )
  76. 					{
  77. 						if ( arg[0] == '-' )
  78. 						{
  79. 							switchcharunix += 1;
  80. 						}
  81. 						else
  82. 						{
  83. 							switchchardos += 1;
  84. 						}
  85. 						switchchar = ( switchcharunix > switchchardos ? '-' : '/' );
  86.  
  87. 						switch ( arg[1].ToString( ).ToUpper( ) )
  88. 						{
  89. 							case "?":
  90. 							case "H":
  91. 							case "-":
  92. 								return ErrorMessage( );
  93. 							case "A":
  94. 								if ( useAllPrn )
  95. 								{
  96. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  97. 								}
  98. 								if ( printerisset )
  99. 								{
  100. 									return ErrorMessage( "Duplicate printer arguments \"{0}\" and \"{1}\"", printer, SwitchString( arg[1] ) );
  101. 								}
  102. 								useAllPrn = true;
  103. 								useDefault = false;
  104. 								useRegex = false;
  105. 								printer = SwitchString( arg[1] );
  106. 								printerisset = true;
  107. 								break;
  108. 							case "D":
  109. 								if ( useDefault )
  110. 								{
  111. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  112. 								}
  113. 								if ( printerisset )
  114. 								{
  115. 									return ErrorMessage( "Duplicate printer arguments \"{0}\" and \"{1}\"", printer, SwitchString( arg[1] ) );
  116. 								}
  117. 								useDefault = true;
  118. 								useAllPrn = false;
  119. 								useRegex = false;
  120. 								printer = SwitchString( arg[1] );
  121. 								printerisset = true;
  122. 								break;
  123. 							case "F":
  124. 								if ( actionFlush )
  125. 								{
  126. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  127. 								}
  128. 								if ( actionisset )
  129. 								{
  130. 									return ErrorMessage( "Duplicate action arguments \"{0}\" and \"CancelAllJobs\"", action );
  131. 								}
  132. 								action = "CancelAllJobs";
  133. 								actionisset = true;
  134. 								actionFlush = true;
  135. 								break;
  136. 							case "L":
  137. 								if ( actionList )
  138. 								{
  139. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  140. 								}
  141. 								if ( actionisset )
  142. 								{
  143. 									return ErrorMessage( "Duplicate action arguments \"{0}\" and \"List\"", action );
  144. 								}
  145. 								action = "List";
  146. 								actionList = true;
  147. 								actionisset = true;
  148. 								break;
  149. 							case "P":
  150. 								if ( actionPause )
  151. 								{
  152. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  153. 								}
  154. 								if ( actionisset )
  155. 								{
  156. 									return ErrorMessage( "Duplicate action arguments \"{0}\" and \"Pause\"", action );
  157. 								}
  158. 								action = "Pause";
  159. 								actionPause = true;
  160. 								actionisset = true;
  161. 								break;
  162. 							case "R":
  163. 								if ( actionResume )
  164. 								{
  165. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  166. 								}
  167. 								if ( actionisset )
  168. 								{
  169. 									return ErrorMessage( "Duplicate action arguments \"{0}\" and \"Resume\"", action );
  170. 								}
  171. 								action = "Resume";
  172. 								actionResume = true;
  173. 								actionisset = true;
  174. 								break;
  175. 							case "S":
  176. 								if ( sortisset )
  177. 								{
  178. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  179. 								}
  180. 								if ( arg.Length == 2 )
  181. 								{
  182. 									sortby = "N";
  183. 								}
  184. 								else if ( arg.Length == 4 && arg[2] == ':' && "JNSjns".Contains( arg[3] ) )
  185. 								{
  186. 									sortby = arg[3].ToString( ).ToUpper( );
  187. 								}
  188. 								else if ( arg[2] == ':' )
  189. 								{
  190. 									return ErrorMessage( "Invalid sort-by value \"{0}\"", arg );
  191. 								}
  192. 								else
  193. 								{
  194. 									return ErrorMessage( "Invalid command line switch {0}", arg.ToUpper( ) );
  195. 								}
  196. 								sortisset = true;
  197. 								break;
  198. 							case "X":
  199. 								if ( useRegex )
  200. 								{
  201. 									return ErrorMessage( "Duplicate command line switch {0}", SwitchString( arg[1] ) );
  202. 								}
  203. 								if ( printerisset )
  204. 								{
  205. 									return ErrorMessage( "Duplicate printer arguments \"{0}\" and \"{1}\"", printer, SwitchString( arg[1] ) );
  206. 								}
  207. 								if ( arg.Length == 2 )
  208. 								{
  209. 									return ErrorMessage( "No regex pattern specified with {0}", SwitchString( arg[1] ) );
  210. 								}
  211. 								else if ( arg.Length == 3 && arg[2] == ':' )
  212. 								{
  213. 									return ErrorMessage( "No regex pattern specified with {0}", SwitchString( arg[1] ) );
  214. 								}
  215. 								else if ( arg.Length > 3 && arg[2] == ':' )
  216. 								{
  217. 									pattern = arg.Substring( 3 );
  218. 									if ( !VerifyRegExPattern( pattern ) )
  219. 									{
  220. 										return ErrorMessage( "Invalid regex pattern \"{0}\"", pattern );
  221. 									}
  222. 								}
  223. 								else
  224. 								{
  225. 									return ErrorMessage( "Invalid command line switch {0}", arg.ToUpper( ) );
  226. 								}
  227. 								useAllPrn = false;
  228. 								useDefault = false;
  229. 								useRegex = true;
  230. 								printer = arg;
  231. 								printerisset = true;
  232. 								break;
  233. 							default:
  234. 								return ErrorMessage( "Invalid command line switch {0}", arg.ToUpper( ) );
  235. 						}
  236. 					}
  237. 					else
  238. 					{
  239. 						if ( printerisset )
  240. 						{
  241. 							return ErrorMessage( "Duplicate printer arguments \"{0}\" and \"{1}\"", printer, arg );
  242. 						}
  243. 						printer = arg;
  244. 						useAllPrn = false;
  245. 						useDefault = false;
  246. 						useRegex = false;
  247. 						printerisset = true;
  248. 					}
  249. 				}
  250.  
  251. 				if ( !printerisset )
  252. 				{
  253. 					if ( actionList )
  254. 					{
  255. 						useAllPrn = true;
  256. 						useDefault = false;
  257. 						printerisset = true;
  258. 					}
  259. 					else
  260. 					{
  261. 						return ErrorMessage( "Printer not specified.\n\tSpecify a printer name, or regex pattern ({0}:\"regex\"), or use {1} (All printers) or {2} (Default printer).", SwitchString( "X" ), SwitchString( "A" ), SwitchString( "D" ) );
  262. 					}
  263. 				}
  264. 				if ( !actionisset )
  265. 				{
  266. 					return ErrorMessage( "No action specified.\n\tUse either {0} (List printers), or {1} (Pause printing),\n\tor {2} (Resume printing), or {3} (Flush print jobs).", SwitchString( "L" ), SwitchString( "P" ), SwitchString( "/R" ), SwitchString( "/F" ) );
  267. 				}
  268.  
  269. 				#endregion Command Line Parsing
  270.  
  271. 				#region Enumerate Printers
  272.  
  273. 				string query1;
  274. 				if ( useDefault )
  275. 				{
  276. 					query1 = "SELECT * FROM Win32_Printer WHERE Default='TRUE'"; // default printer
  277. 				}
  278. 				else if ( useAllPrn || useRegex )
  279. 				{
  280. 					query1 = "SELECT * FROM Win32_Printer"; // all printers
  281. 				}
  282. 				else
  283. 				{
  284. 					query1 = String.Format( "SELECT * FROM Win32_Printer WHERE DeviceID='{0}'", printer ); // specific printer
  285. 				}
  286. 				ManagementObjectSearcher searcher1 = new ManagementObjectSearcher( "root\\CIMV2", query1 );
  287.  
  288. 				#endregion Enumerate Printers
  289.  
  290. 				foreach ( ManagementObject queryObj in searcher1.Get( ) )
  291. 				{
  292.  
  293. 					printer = queryObj["DeviceID"].ToString( ); // printer name
  294.  
  295. 					if ( !useRegex || new Regex( pattern, RegexOptions.IgnoreCase ).IsMatch( printer ) )
  296. 					{
  297. 						#region Investigate Current Printer Status
  298.  
  299. 						printerscount += 1;
  300. 						status = (WMIPrinterStatus) Convert.ToUInt32( queryObj["ExtendedPrinterStatus"] ); // printer status
  301. 						string[] properties = new string[3];
  302. 						properties[0] = printer;
  303. 						properties[1] = status.ToString( );
  304. 						// number of queued print jobs
  305. 						string query2 = String.Format( "SELECT * FROM Win32_PrintJob WHERE Name LIKE '{0}, %'", printer );
  306. 						ManagementObjectSearcher searcher2 = new ManagementObjectSearcher( "root\\CIMV2", query2 );
  307. 						try
  308. 						{
  309. 							jobs = Convert.ToUInt32( searcher2.Get( ).Count );
  310. 							properties[2] = jobs.ToString( );
  311. 						}
  312. 						catch
  313. 						{
  314. 							properties[2] = "Unknown";
  315. 						}
  316. 						printerlist.Add( properties );
  317.  
  318. 						#endregion Investigate Current Printer Status
  319.  
  320. 						takeaction = ( ( actionFlush && jobs > 0 ) || ( actionPause && status != WMIPrinterStatus.Paused ) || ( actionResume && status != WMIPrinterStatus.Unknown ) );
  321. 						if ( takeaction )
  322. 						{
  323. 							#region Invoke Action
  324.  
  325. 							WMIActionResult result = (WMIActionResult) Convert.ToUInt32( queryObj.InvokeMethod( action, null ) );
  326. 							WMIPrinterStatus newstatus = WMIPrinterStatus.Unknown;
  327.  
  328. 							#endregion Invoke Action
  329.  
  330. 							#region Investigate New Status After Action
  331.  
  332. 							if ( actionFlush )
  333. 							{
  334. 								searcher2 = new ManagementObjectSearcher( "root\\CIMV2", query2 );
  335. 								jobs = Convert.ToUInt32( searcher2.Get( ).Count );
  336. 							}
  337. 							else
  338. 							{
  339. 								query2 = String.Format( "SELECT * FROM Win32_Printer WHERE DeviceID='{0}'", printer );
  340. 								searcher2 = new ManagementObjectSearcher( "root\\CIMV2", query2 );
  341. 								foreach ( ManagementObject queryObj2 in searcher2.Get( ) )
  342. 								{
  343. 									newstatus = (WMIPrinterStatus) Convert.ToUInt32( queryObj2["ExtendedPrinterStatus"] );
  344. 								}
  345. 							}
  346. 							string[] actions = new string[4];
  347. 							actions[0] = printer;
  348. 							actions[1] = newstatus.ToString( );
  349. 							actions[2] = jobs.ToString( );
  350. 							actions[3] = result.ToString( );
  351. 							actionslist.Add( actions );
  352.  
  353. 							#endregion Investigate New Status After Action
  354. 						}
  355. 					}
  356. 				}
  357.  
  358. 				#region Warn If No Printers Found
  359.  
  360. 				if ( printerscount == 0 )
  361. 				{
  362. 					return ErrorMessage( "No matching printer found, use {0} for a list of valid printer names", ( switchchar == '/' ? "/L" : "-l" ) );
  363. 				}
  364.  
  365. 				#endregion Warn If No Printers Found
  366.  
  367. 				#region Display The results
  368.  
  369. 				return ShowResults( sortby );
  370.  
  371. 				#endregion Display The results
  372. 			}
  373. 			catch ( Exception e )
  374. 			{
  375. #if DEBUG
  376. 				return ErrorMessage( "{0}\n\t{1}", e.Message, e.StackTrace );
  377. #endif
  378. 				return ErrorMessage( e.Message );
  379. 			}
  380. 		}
  381.  
  382.  
  383. 		public static int ShowResults( string sortby )
  384. 		{
  385. 			int index = 0;
  386. 			switch ( sortby )
  387. 			{
  388. 				case "J": // Sort by # jobs
  389. 					index = 2;
  390. 					break;
  391. 				case "N": // Sort by name
  392. 					index = 0;
  393. 					break;
  394. 				case "S": // Sort by status
  395. 					index = 1;
  396. 					break;
  397. 			}
  398. 			if ( sortby == "N" ) // Sort only by name, ascending
  399. 			{
  400. 				printerlist = printerlist
  401. 					.OrderBy( arr => arr[0] )
  402. 					.ToList( );
  403. 				actionslist = actionslist
  404. 					.OrderBy( arr => arr[0] )
  405. 					.ToList( );
  406. 			}
  407. 			else // Sort by status or # jobs, descending, then by name, ascending
  408. 			{
  409. 				printerlist = printerlist
  410. 					.OrderByDescending( arr => arr[index] )
  411. 					.ThenBy( arr => arr[0] )
  412. 					.ToList( );
  413. 				actionslist = actionslist
  414. 					.OrderByDescending( arr => arr[index] )
  415. 					.ThenBy( arr => arr[0] )
  416. 					.ToList( );
  417. 			}
  418.  
  419. 			foreach ( string[] properties in printerlist )
  420. 			{
  421. 				string printer = properties[0];
  422. 				//UInt32 jobs = Convert.ToUInt32( properties[2] );
  423. 				string jobs = properties[2];
  424. 				string status = properties[1];
  425.  
  426. 				// check if the action really was invoked
  427. 				bool takeaction = ( (( jobs != "Unknown" && !String.IsNullOrWhiteSpace(jobs) && jobs != "0" ) && actionFlush ) || ( actionPause && status != WMIPrinterStatus.Paused.ToString( ) ) || ( actionResume && status != WMIPrinterStatus.Unknown.ToString( ) ) );
  428.  
  429. 				if ( takeaction )
  430. 				{
  431. 					string newstatus = WMIPrinterStatus.Other.ToString( );
  432. 					//UInt32 newjobs = 0;
  433. 					string newjobs = "0";
  434. 					string result = WMIActionResult.Other.ToString( );
  435.  
  436. 					foreach ( string[] actions in actionslist )
  437. 					{
  438. 						if ( actions[0] == properties[0] ) // read the actions list for this printer
  439. 						{
  440. 							newstatus = actions[1];
  441. 							//newjobs = Convert.ToUInt32( actions[2] );
  442. 							newjobs = actions[2];
  443. 							result = actions[3];
  444. 						}
  445. 					}
  446.  
  447. 					Console.Write( "Printer   : " );
  448. 					if ( result != WMIActionResult.Success.ToString( ) )
  449. 					{
  450. 						Console.ForegroundColor = ConsoleColor.Yellow; // if unsuccessful, show printer name in yellow
  451. 					}
  452. 					Console.WriteLine( printer );
  453. 					Console.ResetColor( );
  454. 					Console.WriteLine( "Printjobs : {0}", jobs );
  455. 					Console.WriteLine( "Status    : {0}", status );
  456. 					if ( actionFlush )
  457. 					{
  458. 						Console.Write( "Flush printjobs . . . " );
  459. 					}
  460. 					else
  461. 					{
  462. 						Console.Write( "{0} printing . . . ", ( actionPause ? "Pause" : "Resume" ) );
  463. 					}
  464. 					if ( result != WMIActionResult.Success.ToString( ) )
  465. 					{
  466. 						Console.ForegroundColor = ConsoleColor.Red; // if unsuccessful, show result in red
  467. 					}
  468. 					Console.WriteLine( result );
  469. 					Console.ResetColor( );
  470. 					if ( actionFlush )
  471. 					{
  472. 						Console.WriteLine( "Printjobs : {0}", newjobs );
  473. 					}
  474. 					else
  475. 					{
  476. 						Console.WriteLine( "Status    : {0}", newstatus );
  477. 					}
  478. 				}
  479. 				else // no action was invoked, just list status
  480. 				{
  481. 					Console.WriteLine( "Printer   : {0}", printer );
  482. 					Console.WriteLine( "Printjobs : {0}", jobs );
  483. 					Console.WriteLine( "Status    : {0}", status );
  484. 				}
  485. 				Console.WriteLine( );
  486. 			}
  487.  
  488. 			return 0;
  489. 		}
  490.  
  491.  
  492. 		public static string SwitchString( char sw )
  493. 		{
  494. 			return SwitchString( sw.ToString( ) );
  495. 		}
  496.  
  497.  
  498. 		public static string SwitchString( string sw )
  499. 		{
  500. 			if ( switchchar == '-' )
  501. 			{
  502. 				return String.Format( "{0}{1}", switchchar, sw.ToLower( ) );
  503. 			}
  504. 			else
  505. 			{
  506. 				return String.Format( "{0}{1}", switchchar, sw.ToUpper( ) );
  507. 			}
  508. 		}
  509.  
  510.  
  511. 		static bool VerifyRegExPattern( string testpattern )
  512. 		{
  513. 			// Test validity of RegEx pattern
  514. 			// Based on http://stackoverflow.com/questions/218680/can-i-test-if-a-regex-is-valid-in-c-sharp-without-throwing-exception
  515. 			if ( String.IsNullOrWhiteSpace( testpattern ) )
  516. 			{
  517. 				return false;
  518. 			}
  519. 			try
  520. 			{
  521. 				Regex.Match( "", testpattern );
  522. 				return true;
  523. 			}
  524. 			catch ( ArgumentException )
  525. 			{
  526. 				return false;
  527. 			}
  528. 		}
  529.  
  530.  
  531. 		public static int ErrorMessage( params string[] errmsg )
  532. 		{
  533. 			/*
  534. 			Printing.exe,  Version 3.04
  535. 			Pause or resume printing, or flush all queued printjobs on the specified
  536. 			printer(s), or list all printers, their status and number of print jobs
  537.  
  538. 			Usage:    Printing.exe  printer  action  [ option ]
  539.  
  540. 			Printer:  /A         use All printers
  541. 			          /D         use Default printer
  542. 			          /X:"regex" use all printers matching the regular eXpression
  543. 			          name       use the specified printer
  544. 			Action:   /F         Flush queued print jobs
  545. 			          /L         List printer name, status, and number of queued print jobs
  546. 			          /P         Pause printing
  547. 			          /R         Resume printing
  548. 			Option:   /S:(N|S|J) Sort by printer Name (default), Status or # print Jobs
  549.  
  550. 			Examples: PRINTING /D /P       Pause printing on Default printer
  551. 			          PRINTING PDF995 /F   Flush print jobs of the printer named PDF995
  552. 			          PRINTING /A /R       Resume printing on All printers
  553. 			          PRINTING /L /S:J     List all printers, Sort by # queued print Jobs
  554.  
  555. 			Notes:    Use doublequotes if the printer name contains spaces.
  556. 			          With /L and no printer specified, All printers (/A) will be assumed.
  557.  
  558. 			Credits:  LINQ code to sort List of string arrays by Tim Schmelter:
  559. 			          http://stackoverflow.com/questions/23873378#23873402
  560. 			          Test for validity of regex pattern based on code from:
  561. 			          http://stackoverflow.com/questions/218680
  562. 			          PrinterInfo code by Bas van der Woude.
  563.  
  564. 			Written by Rob van der Woude
  565. 			http://www.robvanderwoude.com
  566. 			*/
  567.  
  568. 			if ( errmsg.Length > 0 )
  569. 			{
  570. 				List<string> errargs = new List<string>( errmsg );
  571. 				errargs.RemoveAt( 0 );
  572. 				Console.Error.WriteLine( );
  573. 				Console.ForegroundColor = ConsoleColor.Red;
  574. 				Console.Error.Write( "ERROR:\t" );
  575. 				Console.ForegroundColor = ConsoleColor.White;
  576. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  577. 				Console.ResetColor( );
  578.  
  579. 			}
  580.  
  581. 			Console.Error.WriteLine( );
  582.  
  583. 			Console.Error.WriteLine( "Printing.exe,  Version {0}", progver );
  584.  
  585. 			Console.Error.WriteLine( "Pause or resume printing, or flush all queued printjobs on the specified" );
  586.  
  587. 			Console.Error.WriteLine( "printer(s), or list all printers, their status and number of print jobs" );
  588.  
  589. 			Console.Error.WriteLine( );
  590.  
  591. 			Console.Error.Write( "Usage:    " );
  592. 			Console.ForegroundColor = ConsoleColor.White;
  593. 			Console.Error.WriteLine( "Printing.exe  printer  action  [ option ]" );
  594. 			Console.ResetColor( );
  595.  
  596. 			Console.Error.WriteLine( );
  597.  
  598. 			Console.Error.Write( "Printer:  " );
  599. 			Console.ForegroundColor = ConsoleColor.White;
  600. 			Console.Error.Write( "{0}", SwitchString( "A" ) );
  601. 			Console.ResetColor( );
  602. 			Console.Error.Write( "         use " );
  603. 			Console.ForegroundColor = ConsoleColor.White;
  604. 			Console.Error.Write( "A" );
  605. 			Console.ResetColor( );
  606. 			Console.Error.WriteLine( "ll printers" );
  607.  
  608. 			Console.ForegroundColor = ConsoleColor.White;
  609. 			Console.Error.Write( "          {0}", SwitchString( "D" ) );
  610. 			Console.ResetColor( );
  611. 			Console.Error.Write( "         use " );
  612. 			Console.ForegroundColor = ConsoleColor.White;
  613. 			Console.Error.Write( "D" );
  614. 			Console.ResetColor( );
  615. 			Console.Error.WriteLine( "efault printer" );
  616.  
  617. 			Console.ForegroundColor = ConsoleColor.White;
  618. 			Console.Error.Write( "          {0}:\"regex\"", SwitchString( "X" ) );
  619. 			Console.ResetColor( );
  620. 			Console.Error.Write( " use all printers matching the regular e" );
  621. 			Console.ForegroundColor = ConsoleColor.White;
  622. 			Console.Error.Write( "X" );
  623. 			Console.ResetColor( );
  624. 			Console.Error.WriteLine( "pression" );
  625.  
  626. 			Console.ForegroundColor = ConsoleColor.White;
  627. 			Console.Error.Write( "          name" );
  628. 			Console.ResetColor( );
  629. 			Console.Error.WriteLine( "       use the specified printer" );
  630.  
  631. 			Console.Error.Write( "Action:   " );
  632. 			Console.ForegroundColor = ConsoleColor.White;
  633. 			Console.Error.Write( "{0}         F", SwitchString( "F" ) );
  634. 			Console.ResetColor( );
  635. 			Console.Error.WriteLine( "lush queued print jobs" );
  636.  
  637. 			Console.ForegroundColor = ConsoleColor.White;
  638. 			Console.Error.Write( "          {0}         L", SwitchString( "L" ) );
  639. 			Console.ResetColor( );
  640. 			Console.Error.WriteLine( "ist printer name, status, and number of queued print jobs" );
  641.  
  642. 			Console.ForegroundColor = ConsoleColor.White;
  643. 			Console.Error.Write( "          {0}         P", SwitchString( "P" ) );
  644. 			Console.ResetColor( );
  645. 			Console.Error.WriteLine( "ause printing" );
  646.  
  647. 			Console.ForegroundColor = ConsoleColor.White;
  648. 			Console.Error.Write( "          {0}         R", SwitchString( "R" ) );
  649. 			Console.ResetColor( );
  650. 			Console.Error.WriteLine( "esume printing" );
  651.  
  652. 			Console.Error.Write( "Option:   " );
  653. 			Console.ForegroundColor = ConsoleColor.White;
  654. 			Console.Error.Write( "{0} S", SwitchString( "S:(N|S|J)" ) );
  655. 			Console.ResetColor( );
  656. 			Console.Error.Write( "ort by printer " );
  657. 			Console.ForegroundColor = ConsoleColor.White;
  658. 			Console.Error.Write( "N" );
  659. 			Console.ResetColor( );
  660. 			Console.Error.Write( "ame (default), " );
  661. 			Console.ForegroundColor = ConsoleColor.White;
  662. 			Console.Error.Write( "S" );
  663. 			Console.ResetColor( );
  664. 			Console.Error.Write( "tatus or # print " );
  665. 			Console.ForegroundColor = ConsoleColor.White;
  666. 			Console.Error.Write( "J" );
  667. 			Console.ResetColor( );
  668. 			Console.Error.WriteLine( "obs" );
  669.  
  670. 			Console.Error.WriteLine( );
  671.  
  672. 			Console.Error.Write( "Examples: " );
  673. 			Console.ForegroundColor = ConsoleColor.White;
  674. 			Console.Error.Write( "PRINTING {0} {1}       P", SwitchString( "D" ), SwitchString( "P" ) );
  675. 			Console.ResetColor( );
  676. 			Console.Error.Write( "ause printing on " );
  677. 			Console.ForegroundColor = ConsoleColor.White;
  678. 			Console.Error.Write( "D" );
  679. 			Console.ResetColor( );
  680. 			Console.Error.WriteLine( "efault printer" );
  681.  
  682. 			Console.ForegroundColor = ConsoleColor.White;
  683. 			Console.Error.Write( "          PRINTING PDF995 {0}   F", SwitchString( "F" ) );
  684. 			Console.ResetColor( );
  685. 			Console.Error.Write( "lush print jobs of the printer named " );
  686. 			Console.ForegroundColor = ConsoleColor.White;
  687. 			Console.Error.WriteLine( "PDF995" );
  688. 			Console.ResetColor( );
  689.  
  690. 			Console.ForegroundColor = ConsoleColor.White;
  691. 			Console.Error.Write( "          PRINTING {0} {1}       R", SwitchString( "A" ), SwitchString( "R" ) );
  692. 			Console.ResetColor( );
  693. 			Console.Error.Write( "esume printing on " );
  694. 			Console.ForegroundColor = ConsoleColor.White;
  695. 			Console.Error.Write( "A" );
  696. 			Console.ResetColor( );
  697. 			Console.Error.WriteLine( "ll printers" );
  698.  
  699. 			Console.ForegroundColor = ConsoleColor.White;
  700. 			Console.Error.Write( "          PRINTING {0} {1}     L", SwitchString( "L" ), SwitchString( "S:J" ) );
  701. 			Console.ResetColor( );
  702. 			Console.Error.Write( "ist all printers, " );
  703. 			Console.ForegroundColor = ConsoleColor.White;
  704. 			Console.Error.Write( "S" );
  705. 			Console.ResetColor( );
  706. 			Console.Error.Write( "ort by # queued print " );
  707. 			Console.ForegroundColor = ConsoleColor.White;
  708. 			Console.Error.Write( "J" );
  709. 			Console.ResetColor( );
  710. 			Console.Error.WriteLine( "obs" );
  711.  
  712. 			Console.Error.WriteLine( );
  713.  
  714. 			Console.Error.WriteLine( "Notes:    Use doublequotes if the printer name contains spaces." );
  715.  
  716. 			Console.Error.Write( "          With " );
  717. 			Console.ForegroundColor = ConsoleColor.White;
  718. 			Console.Error.Write( SwitchString( "L" ) );
  719. 			Console.ResetColor( );
  720. 			Console.Error.Write( " and no printer specified, " );
  721. 			Console.ForegroundColor = ConsoleColor.White;
  722. 			Console.Error.Write( "A" );
  723. 			Console.ResetColor( );
  724. 			Console.Error.Write( "ll printers (" );
  725. 			Console.ForegroundColor = ConsoleColor.White;
  726. 			Console.Error.Write( SwitchString( "A" ) );
  727. 			Console.ResetColor( );
  728. 			Console.Error.WriteLine( ") will be assumed." );
  729.  
  730. 			Console.Error.WriteLine( );
  731.  
  732. 			Console.Error.WriteLine( "Credits:  LINQ code to sort List of string arrays by Tim Schmelter:" );
  733.  
  734. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  735. 			Console.Error.WriteLine( "          http://stackoverflow.com/questions/23873378#23873402" );
  736. 			Console.ResetColor( );
  737.  
  738. 			Console.Error.WriteLine( "          Test for validity of regex pattern based on code from:" );
  739.  
  740. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  741. 			Console.Error.WriteLine( "          http://stackoverflow.com/questions/218680" );
  742. 			Console.ResetColor( );
  743.  
  744. 			Console.Error.WriteLine( "          PrinterInfo code by Bas van der Woude." );
  745.  
  746. 			Console.Error.WriteLine( );
  747.  
  748. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  749.  
  750. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  751.  
  752. 			return 1;
  753. 		}
  754. 	}
  755.  
  756.  
  757. 	#region PrinterInfo
  758. 	// Author : Bas van der Woude
  759. 	// Date   : 2011-03-31
  760.  
  761. 	public class PrinterInfo
  762. 	{
  763. 		public string Name { get; set; }
  764. 		public uint Status { get; set; }
  765. 		public uint PrintJobCount { get; set; }
  766. 		public WMIActionResult ActionResult { get; set; }
  767. 	}
  768.  
  769.  
  770. 	public enum WMIActionResult
  771. 	{
  772. 		Success = 0,
  773. 		AccessDenied = 5,
  774. 		Other = 8
  775. 	}
  776.  
  777.  
  778. 	public enum WMIPrinterStatus
  779. 	{
  780. 		Other = 1,
  781. 		Unknown = 2,
  782. 		Idle = 3,
  783. 		Printing = 4,
  784. 		Warmup = 5,
  785. 		StoppedPrinting = 6,
  786. 		Offline = 7,
  787. 		Paused = 8,
  788. 		Error = 9,
  789. 		Busy = 10,
  790. 		NotAvailable = 11,
  791. 		Waiting = 12,
  792. 		Processing = 13,
  793. 		Initialization = 14,
  794. 		PowerSave = 15,
  795. 		PendingDeletion = 16,
  796. 		IOActive = 17,
  797. 		ManualFeed = 18
  798. 	}
  799.  
  800. 	#endregion PrinterInfo
  801. }
  802.  

page last uploaded: 2017-08-21, 14:26