Share via


How to: Write Text to a File

The following example shows how to write text to a text file. It reads all the text files from the user's My Documents folder by using a "*.txt" search pattern, and writes them into a large text file.

Note

Visual Basic users may choose to use the methods and properties provided by the Microsoft.VisualBasic.FileIO.FileSystem class for file I/O.

Example

Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Class Program

    Public Shared Sub Main(ByVal args As String())

        Dim mydocpath As String = _
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim sb As New StringBuilder()

        For Each txtName As String _
            In Directory.EnumerateFiles(mydocpath, "*.txt")
            Using sr As New StreamReader(txtName)
                sb.AppendLine(txtName.ToString())
                sb.AppendLine("= = = = = =")
                sb.Append(sr.ReadToEnd())
                sb.AppendLine()
                sb.AppendLine()

            End Using
        Next

        Using outfile As New StreamWriter(mydocpath & "\AllTxtFiles.txt")
            outfile.Write(sb.ToString())
        End Using
    End Sub
End Class
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

class Program
{

    static void Main(string[] args)
    {

        string mydocpath = 
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        StringBuilder sb = new StringBuilder();

        foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt"))
        {
            using (StreamReader sr = new StreamReader(txtName))
            {
                sb.AppendLine(txtName.ToString());
                sb.AppendLine("= = = = = =");
                sb.Append(sr.ReadToEnd());
                sb.AppendLine();
                sb.AppendLine();
            }

        }

        using (StreamWriter outfile = 
            new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
        {
            outfile.Write(sb.ToString());
        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Collections::Generic;

ref class Program
{
public:
    static void Main()
    {
        String^ mydocpath =
            Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);
        StringBuilder^ sb = gcnew StringBuilder();

        for each (String^ txtName in Directory::EnumerateFiles(mydocpath, "*.txt"))
        {
            StreamReader^ sr = gcnew StreamReader(txtName);
            sb->AppendLine(txtName->ToString());
            sb->AppendLine("= = = = = =");
            sb->Append(sr->ReadToEnd());
            sb->AppendLine();
            sb->AppendLine();
            sr->Close();
        }

        StreamWriter^ outfile = gcnew StreamWriter(mydocpath + "\\AllTxtFiles.txt");
        outfile->Write(sb->ToString());
        outfile->Close();
    }
};

int main()
{
    Program::Main();
}

See Also

Tasks

How to: Create a Directory Listing

How to: Read and Write to a Newly Created Data File

How to: Open and Append to a Log File

How to: Read Text from a File

How to: Read Characters from a String

How to: Write Characters to a String

Reference

StreamWriter

File.CreateText

Concepts

Basic File I/O