分享

[C#] 调用CMD,获取系统中的端口列表

 Runs丶SS11 2015-11-13
                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 { }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约