listBox_port.Items.Clear();
Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false;//不借助外壳 p.StartInfo.RedirectStandardInput = true;//允许输入 p.StartInfo.RedirectStandardOutput = true;//允许输出 p.StartInfo.RedirectStandardError = true;//允许报错 p.StartInfo.CreateNoWindow = true;//不创佳窗体,即不弹出黑框 p.Start();//启动进程 p.StandardInput.WriteLine("netstat -an"); p.StandardInput.WriteLine("exit"); string info = p.StandardOutput.ReadToEnd(); File.WriteAllText("port.txt", info); try { string[] port = File.ReadAllLines("port.txt"); for (int i = 8; i < port.Length - 1; i++) { listBox_port.Items.Add(port[i]); string[] ports = port[i].Substring(0, 30).Split(':'); ListViewItem item = new ListViewItem(); item.Text = ports[1]; item.SubItems.Add(port[i].Substring(0, 5)); try { item.SubItems.Add(port[i].Substring(54, port[i].Length-54)); } catch { item.SubItems.Add("暂无"); } listView_port.Items.Add(item); } } catch { } |
|