0

I'm trying to write a c# program that will print a document to a network printer using the System.Printing namespace. The code works on my Windows 7 Desktop, but not on the Windows server.

LocalPrintServer server = new LocalPrintServer(); //To find the nearby printers
PrintQueueCollection queuecollect = server.GetPrintQueues(); //collects all the printer queues on the machine
string queuename = null; //this gets set to the printer we want
foreach (PrintQueue pq in queuecollect)
{
    if (pq.FullName == _printername) //printername is from the config
    {
        queuename = pq.FullName; //pq goes down the list of queues found until it matches
    }
}
_Log.Debug("queuename: " + queuename); //Just so I can see it
PrintQueue queue = new PrintQueue(server, queuename); //this creates a queue for the specified printer on the local machine
byte[] bytes = System.IO.File.ReadAllBytes(dirpath); // The path to the PDF file
try
{
    PrintSystemJobInfo myprintjob = queue.AddJob(); //adds a job to the queue (This is the line 908 mentioned in the error)
    Stream jobstream = myprintjob.JobStream; //creates a stream to write data to the job
    jobstream.Write(bytes, 0, bytes.Length);
    jobstream.Close(); //close it, or else.
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

The code later checks the job status to determine if it printed successfully.

The problem is that when run on the windows server 2012 R2, it generates the following exception:

System.Printing.PrintJobException: An exception occurred while creating print job information. Check inner exception for details.
   at System.Printing.PrintSystemJobInfo..ctor(PrintQueue printQueue, String jobName, PrintTicket printTicket)
   at System.Printing.PrintQueue.AddJob(String jobName)
   at FaxReceiver.PrintFaxes() in c:\(path to Form1.cs):line 908  | 

Line 908 is: PrintSystemJobInfo myprintjob = queue.AddJob(); This does not happen on my Windows 7 desktop. It works perfectly there. Does anyone have any idea why this is happening?

Additional info: Both the desktop and server are 64bit. In both cases, the same printer was used. The file can be printed manually on windows server, so it shouldn't be an issue with the printer. Tried running the program as administrator.

0 Answers0