Útmutató: Naplófájl megnyitása és hozzáfűzése

StreamWriter és StreamReader a streamek karaktereinek írása és olvasása. Az alábbi példakód megnyitja a log.txt fájlt bemenetként, vagy létrehozza, ha nem létezik, és hozzáfűzi a naplófájl adatait a fájl végéhez. A példa ezután a fájl tartalmát szabványos kimenetre írja megjelenítésre.

A példa alternatívaként egyetlen sztringként vagy sztringtömbként tárolhatja az információkat, és a vagy File.WriteAllLines a File.WriteAllText metódus használatával ugyanezt a funkciót érheti el.

Feljegyzés

A Visual Basic felhasználói dönthetnek úgy, hogy az osztály vagy FileSystem osztály Log által biztosított metódusokat és tulajdonságokat használják a naplófájlok létrehozásához vagy írásához.

Példa

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.

Lásd még