C#检测端口是否被占用(转帖)

            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("netstat", "-a");
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            string result = p.StandardOutput.ReadToEnd();
            if (result.IndexOf(Environment.MachineName.ToLower() + ":4000") >= 0)
                MessageBox.Show("4000端口被占用");
            else
            {
                MessageBox.Show("ok");
            }       
原文地址:https://www.cnblogs.com/bayonetxxx/p/1498747.html