Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-remotely-survey-the-status-of-printers.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

7.6 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Remotely Survey the Status of Printers 03/30/2017
csharp
vb
cpp
surveying printer status remotely [WPF]
printer status [WPF], surveying remotely
remotely surveying printer status [WPF]
status [WPF], printers [WPF], surveying remotely
d6324759-8292-4c23-9584-9c708887dc94

How to: Remotely Survey the Status of Printers

At any given time at medium and large companies there may be multiple printers that are not working due to a paper jam or being out of paper or some other problematic situation. The rich set of printer properties exposed in the APIs of Microsoft .NET Framework provide a means for performing a rapid survey of the states of printers.

Example

The major steps for creating this kind of utility are as follows.

  1. Obtain a list of all print servers.

  2. Loop through the servers to query their print queues.

  3. Within each pass of the server loop, loop through all the server's queues and read each property that might indicate that the queue is not currently working.

The code below is a series of snippets. For simplicity, this example assumes that there is a CRLF-delimited list of print servers. The variable fileOfPrintServers is a xref:System.IO.StreamReader object for this file. Since each server name is on its own line, any call of xref:System.IO.StreamReader.ReadLine%2A gets the name of the next server and moves the xref:System.IO.StreamReader's cursor to the beginning of the next line.

Within the outer loop, the code creates a xref:System.Printing.PrintServer object for the latest print server and specifies that the application is to have administrative rights to the server.

Note

If there are a lot of servers, you can improve performance by using the xref:System.Printing.PrintServer.%23ctor%28System.String%2CSystem.String%5B%5D%2CSystem.Printing.PrintSystemDesiredAccess%29 constructors that only initialize the properties you are going to need.

The example then uses xref:System.Printing.PrintServer.GetPrintQueues%2A to create a collection of all of the server's queues and begins to loop through them. This inner loop contains a branching structure corresponding to the two ways of checking a printer's status:

This example demonstrates both methods, so the user was previously prompted as to which method to use and responded with "y" if they wanted to use the flags of the xref:System.Printing.PrintQueue.QueueStatus%2A property. See below for the details of the two methods.

Finally, the results are presented to the user.

[!code-cppPrinterStatusSurvey#SurveyQueues] [!code-csharpPrinterStatusSurvey#SurveyQueues] [!code-vbPrinterStatusSurvey#SurveyQueues]

To check printer status using the flags of the xref:System.Printing.PrintQueue.QueueStatus%2A property, you check each relevant flag to see if it is set. The standard way to see if one bit is set in a set of bit flags is to perform a logical AND operation with the set of flags as one operand and the flag itself as the other. Since the flag itself has only one bit set, the result of the logical AND is that, at most, that same bit is set. To find out whether it is or not, just compare the result of the logical AND with the flag itself. For more information, see xref:System.Printing.PrintQueueStatus, the & Operator (C# Reference), and xref:System.FlagsAttribute.

For each attribute whose bit is set, the code adds a notice to the final report that will be presented to the user. (The ReportAvailabilityAtThisTime method that is called at the end of the code is discussed below.)

[!code-cppPrinterStatusSurvey#SpotTroubleUsingQueueAttributes] [!code-csharpPrinterStatusSurvey#SpotTroubleUsingQueueAttributes] [!code-vbPrinterStatusSurvey#SpotTroubleUsingQueueAttributes]

To check printer status using each property, you simply read each property and add a note to the final report that will be presented to the user if the property is true. (The ReportAvailabilityAtThisTime method that is called at the end of the code is discussed below.)

[!code-cppPrinterStatusSurvey#SpotTroubleUsingQueueProperties] [!code-csharpPrinterStatusSurvey#SpotTroubleUsingQueueProperties] [!code-vbPrinterStatusSurvey#SpotTroubleUsingQueueProperties]

The ReportAvailabilityAtThisTime method was created in case you need to determine if the queue is available at the current time of day.

The method will do nothing if the xref:System.Printing.PrintQueue.StartTimeOfDay%2A and xref:System.Printing.PrintQueue.UntilTimeOfDay%2A properties are equal; because in that case the printer is available at all times. If they are different, the method gets the current time which then has to be converted into total minutes past midnight because the xref:System.Printing.PrintQueue.StartTimeOfDay%2A and xref:System.Printing.PrintQueue.UntilTimeOfDay%2A properties are xref:System.Int32s representing minutes-after-midnight, not xref:System.DateTime objects. Finally, the method checks to see if the current time is between the start and "until" times.

[!code-cppPrinterStatusSurvey#UsingStartAndUntilTimes] [!code-csharpPrinterStatusSurvey#UsingStartAndUntilTimes] [!code-vbPrinterStatusSurvey#UsingStartAndUntilTimes]

See also