• 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;
      }

  • 相关阅读:
    Mysql常用函数总结(二)
    mysql百万的数据快速创建索引
    php 中的sprintf 坑
    php5.5之后新特性整理
    mysql实践总结
    php下载远程图片到本地
    搜藏一个php文件上传类
    Attribute基本介绍
    Fiddler4抓包工具使用教程一
    HTTP传输数据压缩
  • 原文地址:https://www.cnblogs.com/RobotTech/p/645881.html
Copyright © 2020-2023  润新知