News... | Hack-Acad | Downloads | Web-Projekte | System-Check | Kontakt
HACKACAD - C# - IO TXT File

Wie schreibe ich nun ein einfaches Logfile für meine Applikation? Folgendes Beispiel zeigt die minimalen Lese und Schreibvorgänge:

  Source Code:
 
 
 using System;
 using System.Collections;
 using System.ComponentModel;
 using System.Data;
 using System.Diagnostics;
 using System.Threading;
 using System.Windows.Forms;
 using System.IO;

 namespace WriteToFile
 {
   class Class1
   {
      [STAThread]
      static void Main(string[] args)
      {
         string logfile = "log.txt";
         string path = Application.StartupPath + "\\" + logfile;
         Console.WriteLine(path);
         if (!File.Exists(path)) 
         {				
            TextWriter tw = new StreamWriter(path);
            tw.WriteLine(DateTime.Now);
            tw.Close();

            Console.WriteLine("TXT generiert!");
         }	
         else
         {
            Console.WriteLine("TXT existiert bereits! ... Zeile angehängt.");

            using (StreamWriter sw = File.AppendText(path)) 
            {					
               sw.WriteLine("This");
               sw.WriteLine("is Extra");
               sw.WriteLine("Text");
            }
         }

         using(TextReader streamReader =  new StreamReader(path))
         {			
            Console.WriteLine("\r\n {0} - Inhalt \r\n\r\n {1}", path, streamReader.ReadToEnd());
         }

         Console.ReadLine();
      }
   }
 }