/// <summary> /// Waits for installation of the specified queue to be complete. /// </summary> /// <param name="queueName">The name of the queue to wait for.</param> /// <param name="driverName">The name of the driver associated with the queue.</param> /// <exception cref="TimeoutException">A timeout was reached while waiting for installation to complete.</exception> public static void WaitForInstallationComplete(string queueName, string driverName) { // The InstallationComplete flag is only set for UPD drivers. if (string.IsNullOrEmpty(driverName) || !driverName.StartsWith("HP Universal", StringComparison.OrdinalIgnoreCase)) { return; } LogInfo("Waiting for installation to complete..."); using (AutoResetEvent queueWaitEvent = new AutoResetEvent(false)) { using (var watcher = new RegistryValueEventWatcher(RegistryHive.LocalMachine, $@"SYSTEM\\CurrentControlSet\\Control\\Print\\Printers\\{queueName}\\PrinterDriverData", "InstallationComplete")) { watcher.RegistryChanged += (s, e) => { if (PrintRegistryUtil.GetQueueInstallationCompleteStatus(queueName)) { queueWaitEvent.Set(); } }; watcher.Start(); // Perform a manual check in case the value is already set to 0, // or the change slipped in before the event watcher was started. bool alreadyDone = PrintRegistryUtil.GetQueueInstallationCompleteStatus(queueName); if (!alreadyDone && !queueWaitEvent.WaitOne(TimeSpan.FromMinutes(2))) { throw new TimeoutException("Timeout reached waiting for queue."); } } } LogInfo("Installation complete"); }
/// <summary> /// Sets the job render location for the specified print queue. /// </summary> /// <param name="queue">The <see cref="PrintQueue" /> to modify.</param> /// <param name="location">The <see cref="PrintJobRenderLocation" />.</param> /// <exception cref="ArgumentException"><paramref name="location" /> is <see cref="PrintJobRenderLocation.Unknown" /></exception> public static void SetJobRenderLocation(PrintQueue queue, PrintJobRenderLocation location) { // This feature only applies to Vista and greater if (Environment.OSVersion.Version.Major >= 6) { PrintRegistryUtil.SetJobRenderLocation(queue, location); } }
/// <summary> /// Gets the job render location for the specified print queue. /// </summary> /// <param name="queue">The <see cref="PrintQueue" /> to query.</param> /// <returns>The <see cref="PrintJobRenderLocation" /> for the queue.</returns> public static PrintJobRenderLocation GetJobRenderLocation(PrintQueue queue) { // This feature only applies to Vista and greater if (Environment.OSVersion.Version.Major >= 6) { return(PrintRegistryUtil.GetJobRenderLocation(queue)); } else { return(PrintJobRenderLocation.Unknown); } }
/// <summary> /// Sets the default print queue to the specified queue. /// </summary> /// <param name="printQueue">The <see cref="PrintQueue" /> to set as the default.</param> /// <exception cref="ArgumentNullException"><paramref name="printQueue" /> is null.</exception> public static void SetDefaultQueue(PrintQueue printQueue) { if (printQueue == null) { throw new ArgumentNullException(nameof(printQueue)); } LogDebug($"Setting {printQueue.FullName} as the default queue."); string printerPortKey = PrintRegistryUtil.GetPrinterPortValue(printQueue) ?? "winspool,Ne00:"; PrintRegistryUtil.SetDefaultDeviceKey(printQueue, printerPortKey); }
/// <summary> /// Changes the attributes of the specified print queue. /// </summary> /// <param name="printQueue">The <see cref="PrintQueue" /> to modify.</param> /// <param name="attributes">The attributes to modify.</param> /// <param name="setAttributes">if set to <c>true</c> add the specified attributes; otherwise, remove those attributes.</param> /// <exception cref="ArgumentNullException"><paramref name="printQueue" /> is null.</exception> public static void ChangeAttributes(PrintQueue printQueue, PrintQueueAttributes attributes, bool setAttributes) { if (printQueue == null) { throw new ArgumentNullException(nameof(printQueue)); } PrintQueueAttributes originalAttributes = printQueue.QueueAttributes; PrintQueueAttributes modifiedAttributes = setAttributes ? originalAttributes | attributes : originalAttributes & ~attributes; if (modifiedAttributes != originalAttributes) { PrintRegistryUtil.SetPrinterAttributes(printQueue, modifiedAttributes); // The print spooler must be restarted for this change to take effect. PrintSpooler.RestartSpooler(); } }