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?

Ali
  • 3,373
  • 5
  • 42
  • 54
Murali Uppangala
  • 884
  • 6
  • 22
  • 49
  • 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. – Hans Passant Nov 29 '13 at 11:53

2 Answers2

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";
JohnB
  • 18,046
  • 16
  • 98
  • 110
honzakuzel1989
  • 2,400
  • 2
  • 29
  • 32
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();
JohnB
  • 18,046
  • 16
  • 98
  • 110