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