コード例 #1
0
        private void update_Button_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                List <QueuePortData> selected = Selected;

                TraceFactory.Logger.Debug("Begin Updating Port Addresses...");
                if (ValidateInput(selected))
                {
                    StandardTcpIPPort port    = null;
                    StandardTcpIPPort newPort = null;
                    foreach (QueuePortData selectedItem in selected)
                    {
                        port = _installedPorts[selectedItem];

                        /* What we need to do here is change the existing port's name and address.
                         * Since the OS identifies the port by it's name, we have to create a new port,
                         * copy over all of it's properties, point the print queue to the new port,
                         * then delete the old one.
                         * One caveat to note:  The port version needs to ALWAYS be 1 when creating a new port.
                         * Existing ports will show different values for Version.  For example:
                         * Server 2003/XP Version = 1.
                         * Server 2008/Win7 Version = 2.
                         * So, if this code is executing on a Win7 box, it's existing port will show a Version of 2.
                         * However, when creating the new port, the Version needs to be set to 1.  After it is created,
                         * it will display 2. I couldn't find any info to explain why this is.
                         * When using CreateRawPortData, the Version defaults to 1.
                         * All other properties are copied to the newly created port. -kyoungman
                         */
                        newPort               = new StandardTcpIPPort(selectedItem.NewPortAddress, selectedItem.PortNumber);
                        newPort.Protocol      = port.Protocol;
                        newPort.Queue         = selectedItem.QueueName;
                        newPort.SnmpCommunity = port.SnmpCommunity;
                        newPort.SnmpEnabled   = port.SnmpEnabled;
                        newPort.SnmpDevIndex  = port.SnmpDevIndex;

                        newPort.CreatePort();
                        ChangeQueuePort(port.PortName, newPort.PortName); //Point the printer to the new port
                        port.DeletePort();
                    }

                    RefreshGrid();
                }
            }
            catch (InvalidOperationException opEx)
            {
                TraceFactory.Logger.Error(opEx);
                DisplayError();
            }
            catch (ManagementException manEx)
            {
                TraceFactory.Logger.Error(manEx.JoinAllErrorMessages(), manEx);
                DisplayError();
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieves the list of installed ports on this machine and pairs the port data with it's corresponding queue information.
        /// </summary>
        /// <returns></returns>
        private List <QueuePortData> GetQueueData()
        {
            Collection <StandardTcpIPPort> ports  = StandardTcpIPPort.InstalledPorts;
            List <QueuePortData>           result = new List <QueuePortData>();

            _installedPorts = new Dictionary <QueuePortData, StandardTcpIPPort>();

            StandardTcpIPPort port = null;

            foreach (PrintQueue queue in PrintQueueController.GetPrintQueues())
            {
                QueuePortData data = new QueuePortData(queue.Name, queue.QueuePort.Name);
                port = ports.Where(p => p.PortName == data.PortName).FirstOrDefault();
                if (port != null) //Only return the queues that have a port.
                {
                    data.PortAddress = port.Address;
                    data.PortNumber  = unchecked ((int)port.PortNumber);
                    result.Add(data);
                    _installedPorts.Add(data, port);
                }
            }

            return(result);
        }