4

Am trying to print a document on a shared printer; i need to get print queue details.The following code always gets queue from 'Microsoft XPS Document' as Number of jobs=0.But my default printer is configured as 'HP LaserJet P1505n'

LocalPrintServer server = new LocalPrintServer()
PrintQueueCollection queueCollection = server.GetPrintQueues();
PrintQueue printQueue = null;
foreach (PrintQueue pq in queueCollection)
{
 Logger.LogInfo("PrintQueue1", "Printer1 Queue Name " + pq.FullName);
 printQueue = pq;
 numberOfJobs = printQueue.NumberOfJobs;
 Logger.LogInfo("numberOfJobs1"+ numberOfJobs);
}

How to get print queue details from that specific shared printer? i tried following also

PrintServer server = new PrintServer(@"\\192.168.100.168\HP LaserJet P1505n");

but got error as:

Win32 error: The filename, directory name, or volume label syntax is incorrect

What am i missing here?

1
  • 2
    You must use PrintServer, not LocalPrintServer. And use the server name, not the printer name and not the IP address. And have sufficient access rights to it. Ask your LAN admin to help you out. Nov 29, 2013 at 11:53

2 Answers 2

9

How to get print queue details from that specific shared printer?

Try something like this:

// string.Empty or null for local printers
string printServerName = @"\\server";
string printQueueName= "printer";

PrintServer ps = string.IsNullOrEmpty(printServerName)
    // for local printers
    ? new PrintServer()
    // for shared printers
    : new PrintServer(printServerName);
PrintQueue pq = ps.GetPrintQueue(printQueueName);

Console.WriteLine(pq.FullName);
Console.WriteLine(pq.NumberOfJobs);
// output is printer uri (\\server\printer) and 0.

Is also possible use server ip address (like string) instead server name.

string printServerName = @"\\192.168.1.111"; // for example

i.e
for local printer PDFCreator set

string printServerName = null;
string printerName = "PDFCreator";

and for shared printer P on server S set

string printServerName = @"\\S";
string printerName = "P";
2
  • Thank you! but i think at the end PrintServer should get disposed. ps.Dispose(); Mar 9, 2020 at 10:15
  • Your wellcome. Yes, it generally should, because PrintServer's parent has IDisposable interface. Good point! Mar 9, 2020 at 16:10
2

System.Printing.PrintServer class documentation on MSDN

Try this:

var myServer = @"\\192.168.100.168";

With this code:

// Create a PrintServer
// "theServer" must be a print server to which the user has full print access.
// var myServer = @"\\theServer"
PrintServer myPrintServer = new PrintServer(myServer);

// List the print server's queues
PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
String printQueueNames = "My Print Queues:\n\n";
foreach (PrintQueue pq in myPrintQueues)
{
    printQueueNames += "\t" + pq.Name + "\n";
}
Console.WriteLine(printQueueNames);
Console.WriteLine("\nPress Return to continue.");
Console.ReadLine();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.