C#当前打印队列问题

yj01263431 2015-05-19 07:52:21
C#如何获取当前打印队列
如果打印队列有多个任务,如何获取当前打印的队列
我现在只能获取所有的队列
string printName = bus.GetAttrValueByUserName("选择打印机", Business.userName);
PrintServer ps = new PrintServer(PrintSystemDesiredAccess.AdministrateServer);
PrintQueue pq = new PrintQueue(ps, printName, PrintSystemDesiredAccess.AdministratePrinter);
PrintSystemJobInfo jobInfo = null;
PrintSystemJobInfo preJobInfo = null;
while (jobInfo == null)
{
pq.Refresh();
var printJobs = pq.GetPrintJobInfoCollection();
foreach (var printJob in printJobs)
{

}
}
...全文
359 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2015-05-20
  • 打赏
  • 举报
回复
c# 获取和取消本地打印队列
Dictionary<string,
int> GetAllPrinterQueues()
{
    RaiseLog("Getting print queue counts");
    Dictionary<string,
int> TempDict
= new Dictionary<string,
int>();

    PrintServer myPrintServer =
new PrintServer();
// Get all the printers installed on this PC

    // List the print server's queues
    PrintQueueCollection myPrintQueues 
= myPrintServer.GetPrintQueues();
    String printQueueNames =
"My Print Queues:\n\n";
    foreach (PrintQueue pq
in myPrintQueues)
    {
        Saint.StCommon.Wait((decimal)2000);
// 2 seconds of "Application.DoEvents(), not thread sleep
        if (GotPingBack(pq.Name))
        {
            int PGcount
= 0;
            try
            {
                if (pq.NumberOfJobs
> 0)
                {
                    // We know there are jobs.  So we *have* to be able to get the collection at some point
                    DateTime Bailout = DateTime.Now.AddSeconds(10);
// Keep trying for 10 seconds or until I get a valid response
                    string ErrMsg
= "notyetretreived";
                    while (Bailout
> DateTime.Now
&& ErrMsg 
!= string.Empty)
                    {
                        try
                        {
                            var Jobs
= pq.GetPrintJobInfoCollection();
                            Saint.StCommon.Wait((int)2);
                            foreach (PrintSystemJobInfo Job
in Jobs)
                            {
                                PGcount += Job.NumberOfPages;
                                ErrMsg =
string.Empty;
                            }
                        }
                        catch (Exception k)
                        {
                            ErrMsg = k.Message;
                            Console.WriteLine(string.Format("{0}: {1}",
pq.Name, k.Message));
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Exception dork");
            }
            Console.WriteLine(string.Format("{2}\t{0}\t{1}",
pq.Name, PGcount, DateTime.Now.ToString("HH:mm:ss.fff")));
            TempDict.Add(pq.Name, PGcount);
        }
    }
    return TempDict;
}

public bool CancelPrintJob(int printJobID)
{
    // Variable declarations.           

    bool isActionPerformed
= false;           

    string searchQuery;           

    String jobName;            
    char[] splitArr;           

    int prntJobID;           

    ManagementObjectSearcher searchPrintJobs;            
    ManagementObjectCollection prntJobCollection;            
    try           

    {                
        // Query to get all the queued printer jobs.               

        searchQuery =
"SELECT * FROM Win32_PrintJob";               

        // Create an object using the above query.               

        searchPrintJobs =
new ManagementObjectSearcher(searchQuery);               

        // Fire the query to get the collection of the printer jobs.               

        prntJobCollection = searchPrintJobs.Get();               

        // Look for the job you want to delete/cancel.               

        foreach (ManagementObject prntJob
in prntJobCollection)
        {                    
            jobName = prntJob.Properties["Name"].Value.ToString();                   

            // Job name would be of the format [Printer name], [Job ID]                   

            splitArr =
new 
char[1];                   

            splitArr[0]
= Convert.ToChar(",");                   

            // Get the job ID.                   

            prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);                   

            // If the Job Id equals the input job Id, then cancel the job.                   

            if (prntJobID
== printJobID)                   

            {                        
                // Performs a action similar to the cancel                       

                // operation of windows print console                       

                prntJob.Delete();                       

                isActionPerformed =
true;                       

                break;                   

            }                
        }                
        return isActionPerformed;           

    }            
    catch (Exception sysException)           

    {                
        // Log the exception.               

        return
false;           

    }        
}
yj01263431 2015-05-20
  • 打赏
  • 举报
回复
引用 2 楼 hefeng_aspnet 的回复:
c# 获取和取消本地打印队列
Dictionary<string,
int> GetAllPrinterQueues()
{
    RaiseLog("Getting print queue counts");
    Dictionary<string,
int> TempDict
= new Dictionary<string,
int>();

    PrintServer myPrintServer =
new PrintServer();
// Get all the printers installed on this PC

    // List the print server's queues
    PrintQueueCollection myPrintQueues 
= myPrintServer.GetPrintQueues();
    String printQueueNames =
"My Print Queues:\n\n";
    foreach (PrintQueue pq
in myPrintQueues)
    {
        Saint.StCommon.Wait((decimal)2000);
// 2 seconds of "Application.DoEvents(), not thread sleep
        if (GotPingBack(pq.Name))
        {
            int PGcount
= 0;
            try
            {
                if (pq.NumberOfJobs
> 0)
                {
                    // We know there are jobs.  So we *have* to be able to get the collection at some point
                    DateTime Bailout = DateTime.Now.AddSeconds(10);
// Keep trying for 10 seconds or until I get a valid response
                    string ErrMsg
= "notyetretreived";
                    while (Bailout
> DateTime.Now
&& ErrMsg 
!= string.Empty)
                    {
                        try
                        {
                            var Jobs
= pq.GetPrintJobInfoCollection();
                            Saint.StCommon.Wait((int)2);
                            foreach (PrintSystemJobInfo Job
in Jobs)
                            {
                                PGcount += Job.NumberOfPages;
                                ErrMsg =
string.Empty;
                            }
                        }
                        catch (Exception k)
                        {
                            ErrMsg = k.Message;
                            Console.WriteLine(string.Format("{0}: {1}",
pq.Name, k.Message));
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Exception dork");
            }
            Console.WriteLine(string.Format("{2}\t{0}\t{1}",
pq.Name, PGcount, DateTime.Now.ToString("HH:mm:ss.fff")));
            TempDict.Add(pq.Name, PGcount);
        }
    }
    return TempDict;
}

public bool CancelPrintJob(int printJobID)
{
    // Variable declarations.           

    bool isActionPerformed
= false;           

    string searchQuery;           

    String jobName;            
    char[] splitArr;           

    int prntJobID;           

    ManagementObjectSearcher searchPrintJobs;            
    ManagementObjectCollection prntJobCollection;            
    try           

    {                
        // Query to get all the queued printer jobs.               

        searchQuery =
"SELECT * FROM Win32_PrintJob";               

        // Create an object using the above query.               

        searchPrintJobs =
new ManagementObjectSearcher(searchQuery);               

        // Fire the query to get the collection of the printer jobs.               

        prntJobCollection = searchPrintJobs.Get();               

        // Look for the job you want to delete/cancel.               

        foreach (ManagementObject prntJob
in prntJobCollection)
        {                    
            jobName = prntJob.Properties["Name"].Value.ToString();                   

            // Job name would be of the format [Printer name], [Job ID]                   

            splitArr =
new 
char[1];                   

            splitArr[0]
= Convert.ToChar(",");                   

            // Get the job ID.                   

            prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);                   

            // If the Job Id equals the input job Id, then cancel the job.                   

            if (prntJobID
== printJobID)                   

            {                        
                // Performs a action similar to the cancel                       

                // operation of windows print console                       

                prntJob.Delete();                       

                isActionPerformed =
true;                       

                break;                   

            }                
        }                
        return isActionPerformed;           

    }            
    catch (Exception sysException)           

    {                
        // Log the exception.               

        return
false;           

    }        
}
获取所有队列我能获取到,我想获取的是当前打印的那个队列,请问有办法吗
yj01263431 2015-05-19
  • 打赏
  • 举报
回复
没人吗,自己顶

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧