2016-07-14 29 views
0

Ich habe versucht, Kontrollkästchen zu schreiben ein Protokoll, wenn Kontrollkästchen aktiviert ist.C# Kontrollkästchen Protokolldatei schreiben

Aber ich habe so viele Probleme, so dass ich nicht wusste, was ich als nächstes tun sollte, vermasselte ich Code.

Wenn Sie Bild schauen, gibt es ein Kontrollkästchen, um ein Protokoll zu schreiben, und wenn ich es überprüfen und IP-Adresse eingeben und dann auf "Ping!" Die Schaltfläche muss dann ping sein und gleichzeitig eine Protokolldatei in c: \ Logs \ schreiben.

enter image description here

Mein Code: "Stop"

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) 
     { 

     } 

    } 
} 

Und wenn ich klicken Knopf, dann hört es auf zu pingen + stoppt das Schreiben eines Protokolls.

Best wishes,

KLDesigns

+1

Did u zu debuggen versucht? Wo scheitert Ihr Code mit welcher Ausnahme? – Lara

+0

Es sagt nicht etwas falsch oder etwas, aber meine Methode funktioniert irgendwie nicht, wenn ich es versuche. – KLDesigns

+0

Wissen Sie, wie Sie Ihren Code debuggen können? Was ist mit Ausnahmebehandlung? Niemand kann Ihnen helfen, es sei denn, Sie sind nicht bereit, sich selbst – Lara

Antwort

0

Versuchen Sie, diese Zeile zu ändern:

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

Um

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

Fehler \t CS0103 \t Der Name 'WriteLine' existiert nicht im aktuellen Kontext – KLDesigns

+0

Entschuldigung, die WriteLog hätte sagen sollen, Antwort aktualisiert –

+0

Vielen Dank so viel !! – KLDesigns