public static bool CancelAllPrintJobs(PrintJobTitle printJob, string host) { try { LogHelper.LogDebug("Need to Cancel at " + host); bool isActionPerformed = false; ManagementScope scope = new ManagementScope("\\\\" + host + "\\root\\cimv2"); scope.Connect(); ObjectQuery query = new ObjectQuery(_searchQueryPrintJobs); ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(scope, query); ManagementObjectCollection prntJobCollection = searchPrintJobs.Get(); int cancelCount = 0; foreach (ManagementObject prntJob in prntJobCollection) { PrintJobTitle _titleX = new PrintJobTitle(prntJob); LogHelper.LogDebug(host + " has to Cancel " + _titleX + " | " + printJob); if (_titleX.Equals(printJob)) { LogHelper.LogDebug("Cancelling at " + host); //performs an action similar to the cancel operation of windows print console prntJob.Delete(); isActionPerformed = true; cancelCount++; } } LogHelper.LogDebug("Cancelled " + cancelCount + " | " + host); return(isActionPerformed); } catch (Exception ex) { LogHelper.Log(ex); return(false); } }
private static bool _PauseResumePrintJobLocal(PrintJobTitle title, string methodName) { try { LogHelper.LogDebug("Local Need to " + methodName); bool isActionPerformed = false; ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(_searchQueryPrintJobs); ManagementObjectCollection prntJobCollection = searchPrintJobs.Get(); LogHelper.LogDebug("Local Enumerate Jobs at " + Environment.MachineName + ", " + prntJobCollection.Count); foreach (ManagementObject prntJob in prntJobCollection) { PrintJobTitle _titleX = new PrintJobTitle(prntJob); if (_titleX.Equals(title)) { LogHelper.LogDebug("Local " + methodName.Substring(0, methodName.Length - 1) + "ing..."); prntJob.InvokeMethod(methodName, null); isActionPerformed = true; LogHelper.LogDebug(methodName); break; } } return(isActionPerformed); } catch (Exception ex) { LogHelper.Log(ex); return(false); } }
/// <summary> /// Checks if the print queue of the specified host has the specified title /// </summary> /// <param name="host">host to be inspected</param> /// <param name="title">title to be found</param> /// <returns>true if the title is present; otherwise false</returns> public static bool HasPrintJob(string host, PrintJobTitle title) { if (String.IsNullOrWhiteSpace(host)) { return(false); } if (title == null) { return(false); } try { foreach (ManagementObject item in GetPrintJobs(host, title.PrinterName)) { if (title.Equals(new PrintJobTitle(item))) { return(true); } } } catch (Exception ex) { LogHelper.Log(ex); } return(false); }
/// Methods #region Methods /// <summary> /// Checks if the watcher is watching specified job /// </summary> /// <param name="data"></param> /// <returns></returns> public bool IsWatchingThisJob(PrintJobData data) { if (data == null) { return(false); } //if (_printJob.PrintJobTitle.PrinterName == data.PrintJobTitle.PrinterName && _printJob.PrintJobTitle.Document == data.PrintJobTitle.Document) if (PrintJobTitle.Equals(data.PrintJobTitle)) { return(true); } return(false); }
private static bool _PauseResumePrintJob(PrintJobTitle title, string methodName) { if (title.Host == Environment.MachineName) { return(_PauseResumePrintJobLocal(title, methodName)); } try { LogHelper.LogDebug("Need to " + methodName + " at " + title.Host); bool isActionPerformed = false; ManagementScope scope = new ManagementScope("\\\\" + title.Host + "\\root\\cimv2"); scope.Connect(); ObjectQuery query = new ObjectQuery(_searchQueryPrintJobs); ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(scope, query); ManagementObjectCollection prntJobCollection = searchPrintJobs.Get(); LogHelper.LogDebug("Enumerate Jobs at " + title.Host + ", " + prntJobCollection.Count); foreach (ManagementObject prntJob in prntJobCollection) { PrintJobTitle _titleX = new PrintJobTitle(prntJob); if (_titleX.Equals(title)) { LogHelper.LogDebug(methodName.Substring(0, methodName.Length - 1) + "ing... " + title.Host); prntJob.InvokeMethod(methodName, null); isActionPerformed = true; LogHelper.LogDebug(methodName + " " + title.Host); //break; } } return(isActionPerformed); } catch (Exception ex) { LogHelper.Log(ex); return(false); } }