Как сделать Ping с указанием времени отправки/получения пакета? — Хабр Q&A

C# cmd ping timestamp

I’m trying to figure out how to get work ping timestamp in C#

I tryed also System.DateTime.Today.ToString(“MM-dd-yyyy”) but its sayng in program wrong host.

At the moment ping showing

Form design:

enter image description here

I tried to use this code:

ping -t|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2>nul"

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Net;

namespace PingProgramm
    public partial class Form1 : Form
        public Form1()
        Thread th;
        private void button1_Click(object sender, EventArgs e)
            th = new Thread(thread1);

        public void thread1()
                string command = "/c ping -t "   textBox1.Text   "|cmd /q /v /c (pause&pause)>nul & for /l %a in () do (set /p "   "data"   '='   " && echo(!date! !time! !data!)&ping -n 2"   textBox1.Text   ">nul";
                ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
                Process proc = new Process();
                proc.StartInfo = procStartInfo;
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.RedirectStandardInput = true;
                procStartInfo.RedirectStandardError = true;
                procStartInfo.UseShellExecute = false;
                procStartInfo.CreateNoWindow = true;
                proc.OutputDataReceived  = new DataReceivedEventHandler(proc_OutputDataReceived);
            catch (Exception)
                //if an error occurs with in the try block, it will handled here.
        void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
            if (stop)
                var proc = (Process)sender;

                stop = false; // allows you to spawn a new thread after stopping the first
                proc.SynchronizingObject = this; // puts the form in charge of async communication
                proc.Kill(); // terminates the thread
                proc.WaitForExit(); // thread is killed asynchronously, so this goes here.

            if (e.Data != null)
                string newLine = e.Data.Trim()   Environment.NewLine;
                MethodInvoker append = () => {
                    richTextBox1.Text  = newLine;
                    if (checkBox1.Checked)
        bool firstTime = true;
        private void textBox1_Click(object sender, EventArgs e)
            if (firstTime)
                firstTime = false;

        bool stop = false;
        private void button2_Click(object sender, EventArgs e)
            stop = true;

        public static void WriteLog(string strLog)
            StreamWriter log;
            FileStream fileStream = null;
            DirectoryInfo logDirInfo = null;
            FileInfo logFileInfo;

            string logFilePath = "C:\Logid\";
            logFilePath = logFilePath   "Log-"   System.DateTime.Today.ToString("MM-dd-yyyy")   "."   "txt";
            logFileInfo = new FileInfo(logFilePath);
            logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
            if (!logDirInfo.Exists) logDirInfo.Create();
            if (!logFileInfo.Exists)
                fileStream = logFileInfo.Create();
                fileStream = new FileStream(logFilePath, FileMode.Append);
            log = new StreamWriter(fileStream);

        private void checkBox1_CheckedChanged(object sender, EventArgs e)


I debugged it but it’s not saying any error or something that.

:/>  Не найден gpedit msc в windows 10 - Решение

Как сделать ping с указанием времени отправки/получения пакета?

Необходимо получить строчку типа:
Обмен пакетами с по с 32 байтами данных:
[09:54:01] Ответ от число байт=32 время<1мс TTL=64
[09:54:02] Ответ от число байт=32 время<1мс TTL=64

плюс, желательно записать ее в лог. Решения нужно 2, для Windows и Linux.

То что ниже не предлагать, это издевательство:
time /t >> 1.txt & ping -n 1 xxx.yyy.zzz.www >> 1.txt

UPD: Появилось решение для Windows и для Linux.

Оставьте комментарий