c#System.Printing AddJob()在windows server 2012 r2上生成异常

c#System.Printing AddJob()在windows server 2012 r2上生成异常,c#,windows,printing,windows-server-2012-r2,system.printing,C#,Windows,Printing,Windows Server 2012 R2,System.printing,我正在尝试编写一个c程序,该程序将使用System.Printing命名空间将文档打印到网络打印机。该代码在我的Windows 7桌面上工作,但在Windows服务器上不工作 LocalPrintServer server = new LocalPrintServer(); //To find the nearby printers PrintQueueCollection queuecollect = server.GetPrintQueues(); //collects all the pr

我正在尝试编写一个c程序,该程序将使用System.Printing命名空间将文档打印到网络打印机。该代码在我的Windows 7桌面上工作,但在Windows服务器上不工作

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());
}
代码随后会检查作业状态,以确定其是否已成功打印

问题在于,在windows server 2012 R2上运行时,会生成以下异常:

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  | 

第908行是:PrintSystemJobInfo myprintjob=queue.AddJob(); 这不会发生在我的Windows 7桌面上。它在那里工作得很好。 有人知道为什么会这样吗

其他信息:
桌面和服务器都是64位的。在这两种情况下,使用了相同的打印机。该文件可以在windows server上手动打印,因此打印机不会出现问题。尝试以管理员身份运行该程序。

第908行有什么内容?PrintSystemJobInfo myprintjob=queue.AddJob()@rkane31174你找到答案了吗?