Using StreamWriter

To create a character-based output stream, wrap a Stream object (such as a FileStream) inside a StreamWriter. StreamWriter defines several constructors. One of its most popular
is shown here:
StreamWriter(Stream stream)
Here, stream is the name of an open stream. This constructor throws an ArgumentException if stream is not opened for output and an ArgumentNullException if stream is null. Once created, a StreamWriter automatically handles the conversion of characters to bytes. When you are done with the StreamWriter, you must close it. Closing the StreamWriter also closes the underlying stream.
 
Here is a simple key-to-disk utility that reads lines of text entered at the keyboard and writes them to a file called test.txt. Text is read until the user enters the word “stop”. It uses a FileStream wrapped in a StreamWriter to output to the file.

Example

using System;
using System.IO;
namespace ConsoleApplication1
{
    class KtoD
    {
        static void Main()
        {
            string str;
            FileStream fout;
            // First, open the file stream.
            try
            {
                fout = new FileStream("test.txt", FileMode.Create);
            }
            catch (IOException exc)
            {
                Console.WriteLine("Error Opening File:\n" + exc.Message);
                return;
            }

            // Wrap the file stream in a StreamWriter.
            StreamWriter fstr_out = new StreamWriter(fout);
            try
            {
                Console.WriteLine("Enter text ('stop' to quit).");
                do
                {
                    Console.Write(": ");
                    str = Console.ReadLine();
                    if (str != "stop")
                    {
                        str = str + "\r\n"; // add newline
                        fstr_out.Write(str);
                    }
                } while (str != "stop");
            }
            catch (IOException exc)
            {
                Console.WriteLine("I/O Error:\n" + exc.Message);
            }
            finally
            {
                fstr_out.Close();
            }
        }
    }
}

In some cases, it might be more convenient to open a file directly using StreamWriter. To do so, use one of these constructors:
StreamWriter(string path)
StreamWriter(string path, bool append)

Here, path specifies the name of the file to open, which can include a full path specifier. In the second form, if append is true, then output is appended to the end of an existing file. Otherwise, output overwrites the specified file. In both cases, if the file does not exist, it is created. Also, both throw an IOException if an I/O error occurs. Other exceptions are also possible.
 
Here is the key-to-disk program rewritten so it uses StreamWriter to open the output file:

Example

using System;
using System.IO;
namespace ConsoleApplication1
{
    class KtoD
    {
        static void Main()
        {
            string str;
            StreamWriter fstr_out = null;
            try
            {
                // Open the file, wrapped in a StreamWriter.
                fstr_out = new StreamWriter("test.txt");
                Console.WriteLine("Enter text ('stop' to quit).");
                do
                {
                    Console.Write(": ");
                    str = Console.ReadLine();
                    if (str != "stop")
                    {
                        str = str + "\r\n"; // add newline
                        fstr_out.Write(str);
                    }
                } while (str != "stop");
            }
            catch (IOException exc)
            {
                Console.WriteLine("I/O Error:\n" + exc.Message);
            }
            finally
            {
                if (fstr_out != null) fstr_out.Close();
            }
        }

    }
}