예제 #1
0
        /// <summary>
        /// Checks, if a certain <paramref name="port"/> is opened on the local machine.
        /// </summary>
        /// <remarks>
        /// <para>Original version found at <see>http://stackoverflow.com/questions/570098/in-c-how-to-check-if-a-tcp-port-is-available/></see>.</para>
        /// </remarks>
        /// <exception cref="ArgumentException">Is thrown if the <paramref name="port"/> parameter contains a value less or equal than 0.</exception>
        /// <param name="port">The number of the port to check.</param>
        /// <returns><c>True</c> if the port is opened, otherwise <c>false.</c></returns>
        public static bool IsPortOpened(int port)
        {
            CheckUtil.ThrowIfZeroOrNegativ(() => port);
            var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            var tcpConnInfoArray   = ipGlobalProperties.GetActiveTcpListeners();

            return(tcpConnInfoArray.Any(ep => ep.Port == port));
        }