现在我要用tracert命令跟踪一批域名的路由现在方案是每调用一次tracert命令创建一个线程每个线程产生了cmd.exe 和tracert.exe两个进程
检测几百个域名,电脑一下子就变成了几百个进程问:有没有方法只开一个cmd.exe和tracert.exe循环检测几百个域名

解决方案 »

  1.   

    将需要检测的所有域名存为一个字符串数组,创建一个线程启动cmd,然后一个一个顺序检测就可以了。
      

  2.   

    LS正解,要是还不明白就去百度“c#线程”吧
      

  3.   

    现在主要问题是怎么用c#循环输入命令到cmd.exe
    能否给个例子
      

  4.   

    问题解决了,下以是代码,给以后的人用
    private void GetAllInfo()
            {
                string pattern = @"(ms|\*).*(ms|\*).*(ms|\*).*";
                string patternIp = @"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"; //验证IP            Process proc = new Process();
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.RedirectStandardInput = true;
                //启动cmd命令
                  proc.Start();
               
                StreamReader reader = null;
                //读取URL
                for (int i = 0; i < this.dgvData.Rows.Count; i++)
                { 
                    bool flag = false;
                    int index = this.dgvData.Rows[i].Index;
                    string url = this.dgvData.Rows[i].Cells["url"].Value.ToString();
                    string ip = this.dgvData.Rows[index].Cells["ip"].Value.ToString();
                    string result = this.dgvData.Rows[index].Cells["result"].Value.ToString();
                    this.dgvData.Rows[index].Cells["updatetime"].Value = string.Empty;
                    //状态提示   
                      int k = i + 1;    
                    UpdateState("正在检测第:" + k + " 个域名:" + url);                proc.StandardInput.WriteLine("tracert -d -h 3 " + url); 
                    //proc.StandardInput.WriteLine("exit");  //这句要注释掉
                    reader = proc.StandardOutput;
                    string line = reader.ReadLine();
                    while (!reader.EndOfStream)
                    {
                        if (flag == false)
                        {
                            if (Regex.IsMatch(line, pattern))
                            {
                                Match mc = Regex.Match(line, patternIp);
                                if (!GetIp().Contains(mc.Value))
                                {
                                    if (ip == mc.Value)
                                    {
                                        this.DgvDataInfo(index, mc.Value, result);   //更新DataGridView窗体
                                    }
                                    else
                                    {
                                        string turl = "http://www.ip138.com/ips.asp?ip=" + mc.Value + "&action=2";
                                        string html = _search.GetRemoteHtmlCode(turl);
                                        html = _search.ReplaceEnter(html);
                                        html = _search.GetResult(html);
                                        this.DgvDataInfo(index, mc.Value, html);   //更新DataGridView窗体
                                        _search.Dispose();
                                    }
                                    flag = true;
                                }
                                else if (line.IndexOf("Request") != -1)
                                {
                                    this.DgvDataInfo(index, mc.Value, "Request timed out");   //更新DataGridView窗体
                                    flag = true;
                                }
                            }                    }
                        line = reader.ReadLine();
                        if (line.IndexOf("complete") != -1)
                        {
                            break;
                        }
                    }
                }
                UpdateState("执行完毕!");
                proc.WaitForExit();
                proc.Close();
                reader.Close(); 
            }