zoukankan      html  css  js  c++  java
  • C# PING其他机器

    using System;
    using System.IO;
    using System.Diagnostics;

    public static bool Ping(string remoteHost)
      {
       bool Flag = false;
       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 " + remoteHost;
        proc.StandardInput.WriteLine(dosLine);
        proc.StandardInput.WriteLine("exit");
        while (proc.HasExited == false)
        {
         proc.WaitForExit(500);
        }
        string pingResult = proc.StandardOutput.ReadToEnd();
        if (pingResult.IndexOf("(0% loss)") != -1)
        {
         Flag = true;
        }
        proc.StandardOutput.Close() ;
       }
       catch
       {
       }
       finally
       {
        try
        {
         proc.Close();
         proc.Dispose();
        }
        catch
        {
        }
       }
       return Flag;
      }


      public static bool Connect(string remoteHost, string userName, string passWord)
      {
       if(!Ping(remoteHost))
       {
        return false;
       }
       bool Flag = true;
       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 = @"net use \\" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
        proc.StandardInput.WriteLine(dosLine);
        proc.StandardInput.WriteLine("exit");
        while (proc.HasExited == false)
        {
         proc.WaitForExit(1000);
        }
        string errormsg = proc.StandardError.ReadToEnd();
        if (errormsg != "")
        {
         Flag = false;
        }
        proc.StandardError.Close() ;
       }
       catch
       {
        Flag = false;
       }
       finally
       {
        try
        {
         proc.Close();
         proc.Dispose();
        }
        catch
        {
        }
       }
       return Flag;
      }

  • 相关阅读:
    JAVA基础知识|HTTP协议-两个特性
    JAVA基础知识|TCP/IP协议
    Spring Cloud|高可用的Eureka集群服务
    Hadoop环境搭建|第四篇:hive环境搭建
    C#中Func与Action的理解
    C# lambda表达式
    WPF ControlTemplate
    sublime text3插件安装及使用
    Dev Express之ImageComboBoxEdit,RepositoryItemImageComboBox使用方式
    SQL查询结果增加序列号
  • 原文地址:https://www.cnblogs.com/RobotTech/p/645881.html
Copyright © 2011-2022 走看看