2016-07-14 169 views
0

如果複選框被選中,我試圖執行復選框來寫入日誌。C#複選框日誌文件寫入

但我有這麼多的問題,所以我不知道下一步做什麼我搞砸了代碼。

如果你看圖像有複選框寫日誌,如果我檢查它並輸入IP地址,然後單擊「Ping!」按鈕,然後必須ping,同時將日誌文件寫入c:\ Logs \。

enter image description here

我的代碼: 「住手!」

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) 
     { 
      if(checkBox1.Checked) 
      { 

      } 
      th = new Thread(thread1); 
      th.Start(); 
     } 

     public void thread1() 
     { 
      try 
      { 
       string command = "/c ping -t " + textBox1.Text; 
       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; 
       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:\\Logs\\"; 
      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) 
     { 

     } 

    } 
} 

如果我點擊按鈕,然後停止ping +停止寫入日誌。

最良好的祝願,

KLDesigns

+1

難道ü嘗試調試?你的代碼在什麼異常下失敗? – Lara

+0

它不會說錯或什麼的,但我的方法莫名其妙地不工作,如果我嘗試它。 – KLDesigns

+0

你知道如何調試你的代碼嗎?什麼是異常處理?沒有人可以幫助你,除非你不願意自己 – Lara

回答

0

嘗試改變這一行:

MethodInvoker append =() => richTextBox1.Text += newLine; 

MethodInvoker append =() => { 
     richTextBox1.Text += newLine; 
     if (checkBox1.Checked) 
     { 
      WriteLog(newLine); 
     } 
    }; 
+0

錯誤\t CS0103 \t名稱'WriteLine'在當前上下文中不存在 – KLDesigns

+0

對不起,應該說有WriteLog,回答更新 –

+0

謝謝你這麼多! – KLDesigns