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");
            }       

posted on 2009-06-08 15:21  ATAK  阅读(6689)  评论(8编辑  收藏  举报

导航