日期:2014-05-18 浏览次数:20938 次
#region 加载窗体 private void FrmTest_Load(object sender, EventArgs e) { this.lblAllUrl.Text = "所有的链接" + "【当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】:"; System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; Thread t = new Thread(new ThreadStart(ping)); t.Start(); } #endregion #region 加载连接对象并获得url值,然后以数组的形式返回 private string[] GetData() { try { string connString = ConfigurationManager.ConnectionStrings["connStringLocal"].ConnectionString.ToString(); SqlConnection conn = new SqlConnection(connString); conn.Open(); string sql = "select top(10) RemoteHost,HotelName from dbo.HotelConn where UseWebSrvFlag=1"; SqlCommand command = new SqlCommand(sql, conn); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { txtAllUrl.AppendText(reader[1].ToString() + ":" + reader[0].ToString() + "," + "\r\n"); } txtAllUrl.Text = txtAllUrl.Text.Remove(txtAllUrl.Text.LastIndexOf(','), 1); reader.Close(); conn.Close(); strLeg = txtAllUrl.Text.Split(','); lblAll.Text = "一共:" + strLeg.Length.ToString(); return strLeg; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return null; } #endregion #region 具体的ping方法 protected void ping() { try { ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到 //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe string[] newStrLeg = GetData(); //以 北京前门店:192.168.103.10 形式 int len = newStrLeg.Length; for (int i = 0; i < len; i++) { txtCommand.Text = ""; string[] newNameStrLeg = newStrLeg[i].Split(':'); //将格式分割成:店名[0] url[1] txtNow.Text = newNameStrLeg[1].ToString(); start.Arguments = newNameStrLeg[1].ToString(); start.CreateNoWindow = true;//不显示dos命令行窗口 start.RedirectStandardOutput = true;// start.RedirectStandardInput = true;// start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序 Process p = Process.Start(start); StreamReader reader = p.StandardOutput;//截取输出流 string line = reader.ReadLine();//每次读取一行 while (!reader.EndOfStream) { txtCommand.AppendText(line + "\r\n"); line = reader.ReadLine(); } p.WaitForExit(); p.Close();//关闭进程 reader.Close();//关闭流 Thread.Sleep(500); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } #endregion