public void Main()
{
Process pro = new Process();
// 设置命令行、参数
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.CreateNoWindow = true;
// 启动CMD
pro.Start();
// 运行端口检查命令
pro.StandardInput.WriteLine("netstat -ano");
pro.StandardInput.WriteLine("exit");
// 获取结果
Regex reg = new Regex("\\s+", RegexOptions.Compiled);
string line = null;
while ((line = pro.StandardOutput.ReadLine()) != null)
{
line = line.Trim();
richTextBox1.Text += line + "\n"; //输出行信息
if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
{
line = reg.Replace(line, ",");
string[] arr = line.Split(',');
if (arr[1].EndsWith(":80"))
{
Console.WriteLine("80端口的进程ID:{0}", arr[4]);
int pid = Int32.Parse(arr[4]);
Process pro80 = Process.GetProcessById(pid);
//
}
}
}
}
{
Process pro = new Process();
// 设置命令行、参数
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.CreateNoWindow = true;
// 启动CMD
pro.Start();
// 运行端口检查命令
pro.StandardInput.WriteLine("netstat -ano");
pro.StandardInput.WriteLine("exit");
// 获取结果
Regex reg = new Regex("\\s+", RegexOptions.Compiled);
string line = null;
while ((line = pro.StandardOutput.ReadLine()) != null)
{
line = line.Trim();
richTextBox1.Text += line + "\n"; //输出行信息
if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
{
line = reg.Replace(line, ",");
string[] arr = line.Split(',');
if (arr[1].EndsWith(":80"))
{
Console.WriteLine("80端口的进程ID:{0}", arr[4]);
int pid = Int32.Parse(arr[4]);
Process pro80 = Process.GetProcessById(pid);
//
}
}
}
}