public VirtualPrinter(ISystemConfiguration systemConfiguration, IPrinterSupport printerSupport)
        {
            Precondition.NotNull(systemConfiguration, "systemConfiguration");
            Precondition.NotNull(printerSupport, "printerSupport");

            this.configuration  = systemConfiguration.PrinterConfiguration;
            this.printerSupport = printerSupport;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the printer instance to use a new port
        /// </summary>
        /// <param name="currentPort">The name of the current port in use</param>
        /// <param name="newPort">The new port to listen on</param>
        /// <param name="portCreated">True if the port has already been created</param>
        /// <param name="printerName">The name of the printer</param>
        /// <returns>True if the port was created</returns>
        public bool UpdatePort(string currentPort, string newPort, bool portCreated, string printerName)
        {
            Precondition.NotNull(currentPort, "currentPort");
            Precondition.NotNullOrEmpty(newPort, "newPort");
            Precondition.NotNullOrEmpty(printerName, "printerName");

            bool created = false;

            if (!this.PortExists(newPort))
            {
                this.AddPort(newPort);
                created = true;
            }

            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(string.Format("SELECT * FROM Win32_Printer WHERE Name='{0}'", printerName));

            string priorPort = string.Empty;

            foreach (ManagementBaseObject printer in searcher.Get())
            {
                var portNameObj = printer["PortName"];
                priorPort           = portNameObj == null ? string.Empty : portNameObj.ToString();
                printer["PortName"] = newPort;

                ManagementObject printerObject = printer as ManagementObject;
                if (printerObject != null)
                {
                    printerObject.Put();                        // Call put to save the settings.
                }
            }

            /* If this application instance created the port, and the new one is different,
             * delete the old one */
            if (priorPort != newPort && !string.IsNullOrEmpty(priorPort))
            {
                this.DeletePort(priorPort);
            }

            return(created);
        }