我想实现C#循环ping多个局域网IP的功能,每次ping的信息都显示在richTextBox中,就是在cmd->ping www.baidu.com出现的那种效果一样。现在ping一个IP的可以实现这样的功能。但是循环ping我就不知道应该怎么办了!!!    我把获取的多个IP保存在一个一维数组中,然后循环数组,读取IP,获取结果,不知道这个想法是不是合理?    哪位给指点一下,说下大体的思路好吗?    谢谢!!!

解决方案 »

  1.   

    [DllImport("kernel32.dll", EntryPoint = "WinExec")]
            public static extern int WinExec(
                string lpCmdLine,
                int nCmdShow
            );
            private void button1_Click(object sender, EventArgs e)
            {            WinExec("cmd.exe /C ping 192.168.1.1 >>C:\\a.TXT", 0);
            }
    Process p = new Process();
     p.StartInfo.FileName = "cmd.exe";  p.StandardInput.WriteLine("ping 192.168.1.1");
                p.StandardInput.WriteLine("exit");
                string str = p.StandardOutput.ReadToEnd();
                File.AppendAllText("c:\\a.txt", str);
      

  2.   


                string[] ipList = { "192.168.1.1", "192.168.1.2" };
                Dictionary<string, bool> dicPingResult = new Dictionary<string, bool>();
                foreach (string ip in ipList)
                {
                    using (Ping ping = new Ping())
                    {
                        ping.SendAsync(ip, 1000, null);
                        ping.PingCompleted += (sender, e) =>
                            {
                                dicPingResult[e.Reply.Address.ToString()] =        e.Reply.Status == IPStatus.Success;
                            };
                    }
                }