C# Read and Write Text Files
How to read/write a ansi, utf-8 or unicode text file from/to string
C# contains easy to use functions to read/write a text file.
Basically you can read a text file with a single line of code (if you want).
The functions are included in System.Io, so you need to
add "using System.Io;" to the code.
We also need System.Text for the encodings
("using System.Text;").
The encoding of the text file is important. Common encodings are:
- Encoding.Default: Operation system current ANSI codepage
- Encoding.UTF8: utf-8 format (e.g. used for html pages)
- Encoding.Unicode: Unicode format (utf-16 little endian encoding, a.k.a. UCS-2 LE)
Encoding.UTF8 and Encoding.Unicode adds a BOM (Byte
Order Mark) to the file.
The byte order mark (BOM) is a unicode
character (at start), which signals the encoding of the text stream (file).
Write a String to a Text File (Unicode Encoding)
Write a string to a unicode text file using System.Io.File.WriteAllText:
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
string
filepath = @"c:\temp\testfile.txt";
// equals string filepath = "c:\\temp\\testfile.txt";
string
filetext = "Hello World" + Environment.NewLine + "line2" + Environment.NewLine
+ "line3";
//equals string filetext = "Hello World\r\nline2\r\nline3";
File.WriteAllText(filepath,
filetext, Encoding.Unicode);
}
}
}
The code can be shortened to:
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
File.WriteAllText(@"c:\temp\testfile.txt",
"Hello World\r\nline2\r\nline3", Encoding.Unicode);
}
}
}
The function adds a BOM (Byte Order Mark) to the file.
Write a String to a Text File (utf-8 Encoding)
Write a string to a utf-8 text file using System.Io.File.WriteAllText:
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
File.WriteAllText(@"c:\temp\testfile.txt",
"Hello World\r\nline2\r\nline3", Encoding.UTF8);
}
}
}
The function adds a BOM (Byte Order Mark) to the file.
Write a String to a Text File (ANSI Encoding)
Write a string to a ANSI (current codepage) text file using System.Io.WriteAllText:
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
File.WriteAllText(@"c:\temp\testfile.txt",
"Hello World\r\nline2\r\nline3", Encoding.Default);
// equals File.WriteAllText(@"c:\temp\testfile.txt",
"Hello World\r\nline2\r\nline3");
}
}
}
Read String from a Text File (Unicode, utf-8 and ANSI Encoding)
Read a string from a text file using System.Io.File.ReadAllText:
We don't need to care about the encoding, because the function detects the encoding by reading the BOM (Byte Order Mark).
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
string
filepath = @"c:\temp\testfile.txt";
// equals string filepath = "c:\\temp\\testfile.txt";
// read text file
string filetext = File.ReadAllText(filepath);
// output text to console
Console.WriteLine(filetext);
}
}
}
The code can be shortened to:
using System;
using System.IO;
using System.Text;
namespace TextFileDemo
{
class Program
{
static void Main(string[] args)
{
// read text file and output text to console
Console.WriteLine(File.ReadAllText(@"c:\temp\testfile.txt"));
}
}
}
Read/Write Text File from/to a string array
Using a string array works basically the same way, you just need to use System.Io.File.WriteAllLines and System.Io.File.ReadAllLines.
Sample:
using System;
using System.IO;
using System.Text;
namespace TextFile
{
class Program
{
static void Main(string[] args)
{
string
filepath = @"c:\temp\testfile.txt";
// write all text line text file to string array
string[] textlines = { "line1", "line2", "line3" };
File.WriteAllLines(filepath, textlines, Encoding.Unicode);
// read all text lines to string array
string[] readarray = File.ReadAllLines(filepath);
// output to console (iterate string array)
foreach (string textline in readarray)
{
Console.WriteLine(textline);
}
}
}
}