string tcpPort = this.txtTCP.Text.Trim();
try
{
if (tcpPort == "" || tcpPort == null)
{
this.lblMessage.Text = "";
MessageBox.Show("请输入TCP端口号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
Process process = new Process();
process.StartInfo = new ProcessStartInfo("netstat", "/ano");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
StreamReader reader = process.StandardOutput;
while (!reader.EndOfStream)
{
string msg = reader.ReadLine().ToUpper();
if (msg.IndexOf("TCP") > 0 && msg.IndexOf(":" + tcpPort) > 0)
{
string info=msg.Trim();
this.lblMessage.Text = "TCP进程的PID为:" + info.Substring(info.Length - 5, 5);
MessageBox.Show("TCP:" + tcpPort + "端口被占用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
break;
}
else
{
this.lblMessage.Text = "";
}
}
reader.Close();
process.Close();
}
}
catch (Exception ex)
{
SaveError(FilePath + "TCPError.log", "检测TCP端口:"+tcpPort+"失败!"+ex.Message);
MessageBox.Show("检测TCP端口:"+tcpPort+"失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
try
{
if (tcpPort == "" || tcpPort == null)
{
this.lblMessage.Text = "";
MessageBox.Show("请输入TCP端口号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
Process process = new Process();
process.StartInfo = new ProcessStartInfo("netstat", "/ano");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
StreamReader reader = process.StandardOutput;
while (!reader.EndOfStream)
{
string msg = reader.ReadLine().ToUpper();
if (msg.IndexOf("TCP") > 0 && msg.IndexOf(":" + tcpPort) > 0)
{
string info=msg.Trim();
this.lblMessage.Text = "TCP进程的PID为:" + info.Substring(info.Length - 5, 5);
MessageBox.Show("TCP:" + tcpPort + "端口被占用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
break;
}
else
{
this.lblMessage.Text = "";
}
}
reader.Close();
process.Close();
}
}
catch (Exception ex)
{
SaveError(FilePath + "TCPError.log", "检测TCP端口:"+tcpPort+"失败!"+ex.Message);
MessageBox.Show("检测TCP端口:"+tcpPort+"失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}