ASP.NET中怎样取得dos命令ping的返回值或者有好的办法确认网络的通讯正常。

解决方案 »

  1.   

    public virtual bool Connected
    {
     get
     {
      try
      {
       //检查socket的状态是否可读
       if(m_socket.Connected && m_socket.Poll(0, SelectMode.SelectRead))
       {
        byte[] aByte = new byte[1];
        //因为TCP/IP协议无法精确的判断网络是否可用
        //试读一个字符,Peek参数指定读取的字符不会从缓冲区中移除
        //假如可读则表示连接可用
        if(m_socket.Receive(aByte, 0, 1, SocketFlags.Peek) != 0)
         return true;
        Close("Disconnected.");
        return false;
       }
      }
      catch(SocketException e)
      {
       OnException(e);
      }
      return m_socket.Connected;
     }
    }
      

  2.   

    Process proc = new Process();
            try
            {
                proc.StartInfo.FileName = "cmd.exe";
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardInput = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                string dosLine = @"ping -n 1 " + "192.168.0.9";
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (proc.HasExited == false)
                {
                    proc.WaitForExit(500);
                }
                string pingResult = proc.StandardOutput.ReadToEnd();
                if (pingResult!="")
                {
                }
                proc.StandardOutput.Close();
            }
            catch
            {
            }
            finally
            {
                try
                {
                    proc.Close();
                    proc.Dispose();
                }
                catch
                {
                }
            }
      

  3.   

    有没有vb的。工程是用vb的。3ks
      

  4.   

    Dim proc As New Process
            Try
                proc.StartInfo.FileName = "cmd.exe"
                proc.StartInfo.UseShellExecute = False
                proc.StartInfo.RedirectStandardInput = True
                proc.StartInfo.RedirectStandardOutput = True
                proc.StartInfo.RedirectStandardError = True
                proc.StartInfo.CreateNoWindow = True
                proc.Start()
                Dim dosLine As String = "ping -n 1 192.168.0.9"
                proc.StandardInput.WriteLine(dosLine)
                proc.StandardInput.WriteLine("exit")
                While (Not proc.HasExited)
                    proc.WaitForExit(500)
                End While
                Dim pingResult As String = proc.StandardOutput.ReadToEnd()
                If pingResult <> "" Then
                End If
                proc.StandardOutput.Close()
            Catch ex As Exception
            Finally
                Try
                    proc.Close()
                    proc.Dispose()
                Catch ex As Exception
                End Try
            End Try
      

  5.   

    pingResult即返回结果If pingResult <> "" Then
    中可以填入你的处理