get print queue details for a shared printer
Categories:
Retrieving Print Queue Details for Shared Printers in C#

Learn how to programmatically access and display detailed information about shared print queues using C# and the .NET Framework's System.Printing namespace.
Managing print jobs and understanding printer status is crucial in many enterprise applications. When dealing with shared network printers, obtaining detailed information about their print queues can be particularly challenging. This article will guide you through using the System.Printing
namespace in C# to programmatically enumerate shared print queues and extract valuable details such as printer name, status, number of jobs, and more. We'll cover the necessary steps, provide code examples, and illustrate the process with a Mermaid diagram.
Understanding the System.Printing Namespace
The System.Printing
namespace in .NET provides a comprehensive set of classes for managing print servers, print queues, print jobs, and printer capabilities. It offers a powerful way to interact with the Windows printing subsystem. Key classes we'll be using include:
PrintServer
: Represents a print server, which can be the local machine or a remote server.PrintQueue
: Represents a print queue associated with a printer. This class allows you to query its properties and manage print jobs.PrintQueueCollection
: A collection ofPrintQueue
objects, typically retrieved from aPrintServer
.
flowchart TD A[Start Application] --> B{Create PrintServer Object} B --> C{Get Shared Print Queues Collection} C --> D{Iterate Through Queues} D --> E{Extract Details (Name, Status, Jobs)} E --> F{Display Information} F --> G[End]
Process for retrieving shared print queue details
Accessing Shared Print Queues
To access shared print queues, you first need to instantiate a PrintServer
object. You can specify a remote server name, or use the default constructor for the local print server. Once you have a PrintServer
instance, you can retrieve its collection of print queues. It's important to filter for shared queues if that's your specific requirement, although often all queues are returned and you can check the IsShared
property.
using System;
using System.Printing;
using System.Linq;
public class PrintQueueDetails
{
public static void Main(string[] args)
{
try
{
// Create a local print server object
// For a remote server, use: new PrintServer("\\\\RemoteServerName");
using (PrintServer printServer = new PrintServer())
{
Console.WriteLine($"\n--- Print Queues on {printServer.Name} ---");
// Get the collection of print queues
PrintQueueCollection printQueues = printServer.GetPrintQueues(
new EnumeratedPrintQueueTypes[] { EnumeratedPrintQueueTypes.Shared });
if (printQueues.Count == 0)
{
Console.WriteLine("No shared print queues found.");
return;
}
foreach (PrintQueue printQueue in printQueues)
{
Console.WriteLine($"\nPrinter Name: {printQueue.Name}");
Console.WriteLine($" Description: {printQueue.Description}");
Console.WriteLine($" Location: {printQueue.Location}");
Console.WriteLine($" Status: {printQueue.QueueStatus}");
Console.WriteLine($" Number of Jobs: {printQueue.NumberOfJobs}");
Console.WriteLine($" Is Shared: {printQueue.IsShared}");
Console.WriteLine($" Is Default: {printQueue.IsDefault}");
Console.WriteLine($" Is Paused: {printQueue.IsPaused}");
Console.WriteLine($" Is Printing: {printQueue.IsPrinting}");
Console.WriteLine($" Driver Name: {printQueue.QueueDriver.Name}");
// You can also enumerate print jobs within the queue
// using (PrintJobInfoCollection jobs = printQueue.GetPrintJobInfoCollection())
// {
// Console.WriteLine($" Active Jobs ({jobs.Count}):");
// foreach (PrintSystemJobInfo job in jobs)
// {
// Console.WriteLine($" Job ID: {job.JobIdentifier}, Name: {job.Name}, Status: {job.JobStatus}");
// }
// }
}
}
}
catch (PrintSystemException pse)
{
Console.WriteLine($"Printing System Error: {pse.Message}");
Console.WriteLine("Ensure the Print Spooler service is running and you have appropriate permissions.");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
}
}
C# code to enumerate and display details of shared print queues.
PrintServer
and PrintQueue
objects, it's good practice to wrap them in using
statements. These classes implement IDisposable
, and using
ensures that system resources are properly released after use, preventing potential resource leaks.Handling Permissions and Exceptions
Accessing print queue information, especially on remote servers, requires appropriate permissions. If your application encounters issues, it's often due to insufficient user rights or the Print Spooler service not running. The System.Printing
namespace can throw PrintSystemException
for printing-related errors, so robust error handling is essential. Always ensure the user running the application has permissions to query printer status on the target print server.
System.Printing
namespace is part of the Windows Presentation Foundation (WPF) and might not be available in all .NET project types (e.g., .NET Core or .NET 5+ console applications without specific targeting). Ensure your project targets a compatible framework (e.g., .NET Framework 4.x or .NET 5+ with Windows Compatibility Pack).