Nasıl yazılır: Günlük dosyasını açma ve ekleme
StreamWriter ve StreamReader akışlara karakter yazma ve akışlardan karakterleri okuma. Aşağıdaki kod örneği, log.txt için bir dosya açar veya yoksa oluşturur ve günlük bilgilerini dosyanın sonuna ekler. Örnek daha sonra dosyanın içeriğini görüntülemek için standart çıkışa yazar.
Bu örneğin alternatifi olarak, bilgileri tek bir dize veya dize dizisi olarak depolayarak aynı işlevselliği elde etmek için File.WriteAllText veya File.WriteAllLines yöntemini kullanabilirsiniz.
Not
Visual Basic kullanıcılar, günlük dosyalarını oluşturmak veya yazmak için sınıf veya sınıf tarafından Log sağlanan yöntemleri ve özellikleri kullanmayı FileSystem seçebilir.
Örnek
using System;
using System.IO;
class DirAppend
{
public static void Main()
{
using (StreamWriter w = File.AppendText("log.txt"))
{
Log("Test1", w);
Log("Test2", w);
}
using (StreamReader r = File.OpenText("log.txt"))
{
DumpLog(r);
}
}
public static void Log(string logMessage, TextWriter w)
{
w.Write("\r\nLog Entry : ");
w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
w.WriteLine(" :");
w.WriteLine($" :{logMessage}");
w.WriteLine ("-------------------------------");
}
public static void DumpLog(StreamReader r)
{
string line;
while ((line = r.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
// The example creates a file named "log.txt" and writes the following lines to it,
// or appends them to the existing "log.txt" file:
// Log Entry : <current long time string> <current long date string>
// :
// :Test1
// -------------------------------
// Log Entry : <current long time string> <current long date string>
// :
// :Test2
// -------------------------------
// It then writes the contents of "log.txt" to the console.
Imports System.IO
Class DirAppend
Public Shared Sub Main()
Using w As StreamWriter = File.AppendText("log.txt")
Log("Test1", w)
Log("Test2", w)
End Using
Using r As StreamReader = File.OpenText("log.txt")
DumpLog(r)
End Using
End Sub
Public Shared Sub Log(logMessage As String, w As TextWriter)
w.Write(vbCrLf + "Log Entry : ")
w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}")
w.WriteLine(" :")
w.WriteLine($" :{logMessage}")
w.WriteLine("-------------------------------")
End Sub
Public Shared Sub DumpLog(r As StreamReader)
Dim line As String
line = r.ReadLine()
While Not (line Is Nothing)
Console.WriteLine(line)
line = r.ReadLine()
End While
End Sub
End Class
' The example creates a file named "log.txt" and writes the following lines to it,
' or appends them to the existing "log.txt" file:
' Log Entry : <current long time string> <current long date string>
' :
' :Test1
' -------------------------------
' Log Entry : <current long time string> <current long date string>
' :
' :Test2
' -------------------------------
' It then writes the contents of "log.txt" to the console.
Ayrıca bkz.
- StreamWriter
- StreamReader
- File.AppendText
- File.OpenText
- StreamReader.ReadLine
- Nasıl kullanılır: Dizinleri ve dosyaları numaralar
- Nasıl kullanılır: Yeni oluşturulan bir veri dosyasını okuma ve dosyaya yazma
- Nasıl okunur: Dosyadan metin okuma
- Nasıl olur: Dosyaya metin yazma
- Nasıl okunur: Bir dizeden karakterleri okuma
- Nasıllı: Dizeye karakter yazma
- Dosya ve akış G/Ç