V
V
Victor2017-10-09 16:47:04
Programming
Victor, 2017-10-09 16:47:04

How to check the availability of a computer in the local network in C#?

Hello!
I am making a program to check the operation of (online) computers in LAN.
Google and modifications was able to create the following code:

private Boolean ping(String host)
        {
            using (var p = new Process())
            {
                p.StartInfo.FileName = "ping.exe";
                p.StartInfo.Arguments = "-n 1 " + host;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding("CP866");
                p.Start();

                string output = p.StandardOutput.ReadToEnd();
                Boolean status = output.Contains("TTL=");
                return status;
            }
        }

But I feel that this is a bicycle with a bunch of crutches ...
Is it possible to somehow more simply and elegantly check if the computer is turned on, knowing its ip?
Application in C# for Windows Form (.NET Framework)
PS in Python, for example, this can all be done with a couple of lines:
response = subprocess.call(["ping", "-c", "1", host], stdout=DNULL)
if response == 0:
    print("Доступен!")

I can't believe that everything in C# is so much more cumbersome...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Zolotov, 2017-10-09
@Victor_M

https://msdn.microsoft.com/en-us/library/hb7xxkfx(...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question