0

我正在尝试编写 ac# 程序,该程序将使用 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 服务器上手动打印,因此它不应该是打印机的问题。尝试以管理员身份运行程序。

4

0 回答 0