---
# System prepended metadata

title: C# Ping を実行する 2021-08-10
tags: [ping, C#]

---

---
lang: ja-jp
breaks: true
---
# C# Ping を実行する 2021-08-10

```csharp=
        private string Ping(string ipaddress)
        {
            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(ipaddress, timeout, buffer, options);
            StringBuilder sbr = new StringBuilder();
            if (reply.Status == IPStatus.Success)
            {
                sbr.AppendFormat("Address: {0} "             , reply.Address.ToString());
                sbr.AppendFormat("RoundTrip time: {0} msec " , reply.RoundtripTime);
                sbr.AppendFormat("Time to live: {0} "        , reply.Options.Ttl);
                sbr.AppendFormat("Don't fragment: {0} "      , reply.Options.DontFragment);
                sbr.AppendFormat("Buffer size: {0} "         , reply.Buffer.Length);
            }
            return sbr.ToString();
        }
```

###### tags: `C#` `ping`
