C# 查看端口是否被占用

来源:互联网 发布:淘宝网账户冻结 编辑:程序博客网 时间:2024/04/28 16:40

private bool checkPort()

{

            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().ToLower();
            if (result.IndexOf(Environment.MachineName.ToLower() + ":6663") >= 0)
            {
                MessageBox.Show("端口被占用");
                return false;
            }
            else
            {
                MessageBox.Show("ok");
                return true;

            } 

}

原创粉丝点击