Exemplo n.º 1
0
        private static PortStatusData GetPortStatus(IPAddress address, int port)
        {
            TcpListener    tcpListener = null;
            PortStatusData portStatusData;

            try
            {
                tcpListener = new TcpListener(address, port);
                tcpListener.Start();
                portStatusData = new PortStatusData(port.ToString(), PortStatus.Free);
            }
            catch (SocketException)
            {
                portStatusData = new PortStatusData(port.ToString(), PortStatus.InUse);
            }
            catch (Exception)
            {
                portStatusData = new PortStatusData(port.ToString(), PortStatus.Unknown);
            }
            finally
            {
                tcpListener?.Stop();
            }

            return(portStatusData);
        }
Exemplo n.º 2
0
        private static PortStatusData GetPortStatus(string portName)
        {
            PortStatusData serialPortStatus;

            try
            {
                using var serialPort = new SerialPort(portName);
                serialPort.Open();
                serialPortStatus = new PortStatusData(portName, PortStatus.Free);
            }
            catch (UnauthorizedAccessException)
            {
                serialPortStatus = new PortStatusData(portName, PortStatus.InUse);
            }
            catch (Exception)
            {
                serialPortStatus = new PortStatusData(portName, PortStatus.Unknown);
            }

            return(serialPortStatus);
        }