c# check remote computer is online code example
Example 1: c# check remote computer is online
// using System.Net.NetworkInformation;
private static bool IsMachineUp(string hostName)
{
bool retVal = false;
try
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(hostName, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
retVal = true;
}
}
catch (Exception ex)
{
retVal = false;
Console.WriteLine(ex.Message);
}
return retVal;
}
Example 2: c# check remote computer is online
// using System.Management;
private static bool IsMachineOnline(string hostName)
{
bool retVal = false;
ManagementScope scope = new ManagementScope(string.Format(@"{0}rootcimv2", hostName));
ManagementClass os = new ManagementClass(scope, new ManagementPath("Win32_OperatingSystem"), null);
try
{
ManagementObjectCollection instances = os.GetInstances();
retVal = true;
}
catch (Exception ex)
{
retVal = false;
Console.WriteLine(ex.Message);
}
return retVal;
}