Main Site Documentation

Writing text in file


#1

I’ve make something like this: http://wiki.tinyclr.com/index.php?title=File_System#SD_Cards

The problem is that every time I write in my file, text is overwrited. What I want to do is append text in new line.

This is how I initialise file:

FileStream FileHandle = new FileStream(rootDirectory +
                     @ "\epilog.txt", FileMode.Create);

#2

use FileMode.Append


public enum FileMode
    {
        // Creates a new file. An exception is raised if the file already exists.
        CreateNew = 1,

        // Creates a new file. If the file already exists, it is overwritten.
        Create = 2,

        // Opens an existing file. An exception is raised if the file does not exist.
        Open = 3,

        // Opens the file if it exists. Otherwise, creates a new file.
        OpenOrCreate = 4,

        // Opens an existing file. Once opened, the file is truncated so that its
        // size is zero bytes. The calling process must open the file with at least
        // WRITE access. An exception is raised if the file does not exist.
        Truncate = 5,

        // Opens the file if it exists and seeks to the end.  Otherwise,
        // creates a new file.
        Append = 6,
    }


#3

Use something like this:


using (FileStream stream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
using (TextWriter writer = new StreamWriter(stream))
    {
        writer.WriteLine("Blablabla");
        writer.Flush();
    }