2016-07-14 5 views
-2

私はまたSystem.DateTime.Today.ToString( "MM-DD-YYYY")が、プログラムでそのsayngしようと試みたのC#C#CMDのPingタイムスタンプ

で作業ピングタイムスタンプを取得する方法を把握しようとしています間違ったホスト。

フォームデザイン示すモーメントのpingで

enter image description here

私はこのコードを使用しようとしました:

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

マイコード:

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() 
     { 
      InitializeComponent(); 
     } 
     Thread th; 
     private void button1_Click(object sender, EventArgs e) 
     { 
      th = new Thread(thread1); 
      th.Start(); 
     } 

     public void thread1() 
     { 
      try 
      { 
       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); 
       proc.Start(); 
       proc.BeginOutputReadLine(); 
       proc.WaitForExit(); 
      } 
      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) 
        { 
         WriteLog(newLine); 
        } 
       }; 
       richTextBox1.BeginInvoke(append); 
      } 
     } 
     bool firstTime = true; 
     private void textBox1_Click(object sender, EventArgs e) 
     { 
      if (firstTime) 
      { 
       firstTime = false; 
       textBox1.Clear(); 
      } 
     } 

     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(); 
      } 
      else 
      { 
       fileStream = new FileStream(logFilePath, FileMode.Append); 
      } 
      log = new StreamWriter(fileStream); 
      log.WriteLine(strLog); 
      log.Close(); 
     } 

     private void checkBox1_CheckedChanged(object sender, EventArgs e) 
     { 

     } 
    } 
} 

私はそれをデバッグしましたが、それは何かエラーや何かを言っていません。

+1

きっとあなたはネイティブ(Ping.Sendを())にping場合は、簡単にそれをフォーマットすることができますしかし、あなたは「proc_ErrorDataReceived」は現在のコンテキストで使用すると、避難所ためだ –

答えて

0

コマンドが実行されたスレッドが例外をスワップするため、エラーは発生しません。

public void thread1() 
    { 
     try 
     { 
      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); 
      proc.ErrorDataReceived+= new DataReceivedEventHandler(proc_ErrorDataReceived); // add error output 
      proc.Start(); 
      proc.BeginOutputReadLine(); 
      proc.BeginErrorReadLine(); // begin read error output 
      proc.WaitForExit(); 
     } 
     catch (Exception ex) // add ex here so you can look into it. 
     { // <== Breakpoint here 
      //if an error occurs with in the try block, it will handled here. 
     } 
    } 

これをデバッグするために、複数の方法があります。

  1. 上記の行にブレークポイントを設定します。

  2. [すべての例外を有効にする]を選択し、[共通言語ランタイム例外]が完全にチェックされるまでチェックします。

  3. また、プロセスはErrorDataReceivedを確認する必要があります。

+0

エラー\t CS0103 \t名前が好き – KLDesigns

+0

@KLDesigns存在しない – stuartd

+0

'void proc_ErrorDataReceived(オブジェクト送信者、DataReceivedEventArgs e) { Console.WriteLine("標準エラーから受信しました: "+ e.Data); } ' – KLDesigns

関連する問題