博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#局域网文件搬移
阅读量:6588 次
发布时间:2019-06-24

本文共 2150 字,大约阅读时间需要 7 分钟。

///
kongxiang--2013.7.23
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ping2
{
    class Program
    {
        //返回true则代表可以ping成功
        //remoteHost为对方IP
        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)
                {
                    proc.WaitForExit(500);
                }
                string pingResult = proc.StandardOutput.ReadToEnd();
                if (pingResult.IndexOf("(0% loss)") != -1)
                {
                    Flag = true;
                }
                proc.StandardOutput.Close();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }
        //返回true则代表可以连接成功
        //remoteHost为对方IP userName为用户名 passWord为密码
        public static bool Connect(string remoteHost, string userName, string passWord)
        {
            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 = @"net use //" + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                string errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
                if (String.IsNullOrEmpty(errormsg))
                {
                    Flag = true;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }
        static void Main(string[] args)
        {
            String strIP = "127.0.0.1";
            Console.WriteLine("Ping Result:{0}", Ping(strIP));
            Console.WriteLine("Connect Result:{0}", Connect("127.0.0.1", "rocher", "123456"));
            Console.Read();
        }
      
    }
}

 

转载于:https://www.cnblogs.com/alwaysfly/p/cmd.html

你可能感兴趣的文章
Shadowbox
查看>>
【 程 序 员 】:伤不起的三十岁,你还有多远 ?
查看>>
openldap安装
查看>>
[leetcode]count and say
查看>>
润乾报表 - 缓存问题
查看>>
利用IFormattable接口自动参数化Sql语句
查看>>
泛型Dictionary的用法详解
查看>>
明晰三种常见存储技术:DAS、SAN和NAS
查看>>
ContentProvider简单介绍
查看>>
Visual Studio 2014 CTPs 下载 和C# 6.0 语言预览版介绍
查看>>
js混淆 反混淆 在线
查看>>
WinForm 之 程序启动不显示主窗体
查看>>
FragmentTransaction.replace() 你不知道的坑
查看>>
模拟退火算法
查看>>
StringUtils方法全集介绍
查看>>
性能调校
查看>>
VMware workstation虚拟网卡类型介绍
查看>>
C# web 更新折腾记
查看>>
IBM主机巡检操作文档
查看>>
zabbix企业应用之Mysql主从监控
查看>>