You need to create a file-possibly for logging information to or storing temporary information-and then write information to it. You also need to be able to read the information that you wrote to this file. To create, write to, and read from a log file, we will use the FileStream and its reader and writer classes. For example, we will create methods to allow construction, reading to, and writing from a log file. To create a log file, you can use the following code:

public FileStream CreateLogFile(string logFileName)
{
    FileStream fileStream = new FileStream(logFileName,
    FileMode.Create,
    FileAccess.ReadWrite,
    FileShare.None);
    return (fileStream);
}

To write text to this file, you can create a StreamWriter object wrapper around the previously created FileStream object (fileStream). You can then use the WriteLine method of the StreamWriter object. The following code writes three lines to the file: a string, followed by an integer, followed by a second string:

public void WriteToLog(FileStream fileStream, string data)
{
    // make sure we can write to this stream
    if (!fileStream.CanWrite)
    {
        // close it and reopen for append
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Append);
    }
    StreamWriter streamWriter = new StreamWriter(fileStream);
    streamWriter.WriteLine(data);
    streamWriter.Close();
}

Now that the file has been created and data has been written to it, you can read the data from this file. To read text from a file, create a StreamReader object wrapper around the file. If the code had not closed the FileStream object (fileStream), it could use that object in place of the filename used to create the StreamReader. To read the entire file in as a single string, use the ReadToEnd method:

public string ReadAllLog(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    StreamReader streamReader = new StreamReader(fileStream);
    string contents = streamReader.ReadToEnd();
    streamReader.Close();
    return contents;
}

If you need to read the lines in one by one, use the Peek method, as shown in ReadLogPeeking, or the ReadLine method, as shown in ReadLogByLines:

public static void ReadLogPeeking(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    Console.WriteLine("Reading file stream peeking at next line:");
    StreamReader streamReader = new StreamReader(fileStream);
    while (streamReader.Peek() != -1)
    {
        Console.WriteLine(streamReader.ReadLine());
    }
    streamReader.Close();
}

or

public static void ReadLogByLines(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    Console.WriteLine("Reading file stream as lines:");
    StreamReader streamReader = new StreamReader(fileStream);
    string text = "";
    while ((text = streamReader.ReadLine()) != null)
    {
        Console.WriteLine(text);
    }
    streamReader.Close();
}

If you need to read in each character of the file as a byte value, use the Read method, which returns a byte value:

public static void ReadAllLogAsBytes(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    Console.WriteLine("Reading file stream as bytes:");
    StreamReader streamReader = new StreamReader(fileStream);
    while (streamReader.Peek() != -1)
    {
        Console.Write(streamReader.Read());
    }
    streamReader.Close();
}

This method displays numeric byte values instead of the characters that they represent. For example, if the log file contained the following text:

This is the first line.
100
This is the third line.

it would be displayed by the ReadAllLogAsBytes method, as follows:

841041051153210511532116104101321021051141151163210810
511010146131049484813108410410511532105115321161041013
211610410511410032108105110101461310

If you need to read in the file by chunks, create and fill a buffer of an arbitrary length based on your performance needs. This buffer can then be displayed or manipulated as needed:

public static void ReadAllBufferedLog(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    Console.WriteLine("Reading file stream as buffers of bytes:");
    StreamReader streamReader = new StreamReader(fileStream);
    while (streamReader.Peek() != -1)
    {
        char[] buffer = new char[10];
        int bufferFillSize = streamReader.Read(buffer, 0, 10);
        foreach (char c in buffer)
        {
            Console.Write(c);
        }
        Console.WriteLine(bufferFillSize);
    }
    streamReader.Close();
}

This method displays the log file’s characters in 10-character chunks, followed by the number of characters actually read. For example, if the log file contained the following text:

This is the first line.
100
This is the third line.

it would be displayed by the ReadAllBufferedLog method as follows:

This is th10
e first li10
ne.
100
10
This is th10
e third li10
ne.
5

Notice that at the end of every tenth character (the buffer is a char array of size 10), the number of characters read in is displayed. During the last read performed, only five characters were left to read from the file. In this case, a 5 is displayed at the end of the text, indicating that the buffer was not completely filled.

The previous code could have been modified to use the ReadBlock method as shown in the ReadAllBufferedLogBlock method instead of the Read method. The output is the same in both cases:

public static void ReadAllBufferedLogBlock(FileStream fileStream)
{
    if (!fileStream.CanRead)
    {
        // close it and reopen for read
        string fileName = fileStream.Name;
        fileStream.Close();
        fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    }
    Console.WriteLine("Reading file stream as buffers of bytes using ReadBlock:");
    StreamReader streamReader = new StreamReader(fileStream);
    while (streamReader.Peek() != -1)
    {
        char[] buffer = new char[10];
        int bufferFillSize = streamReader.ReadBlock(buffer, 0, 10);
        foreach (char c in buffer)
        {
            Console.Write(c);
        }
        Console.WriteLine(bufferFillSize);
    }
    streamReader.Close();
}

This displays the following text:

This is th10
e first li10
ne.
100
10
This is th10
e third li10
ne.
5

Popularity: 3% [?]