C# File Methods

About This Index
This page is the C# File Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. All examples use a small demo folder inside Path.GetTempPath(), remove it at the end, and print only file names, text, counts, and true and false facts, so the output you see is the output you get.
Learn what the File class is for on /c-sharp/file. Use this page when you need a specific API such as WriteAllText(), ReadAllText(), or Exists().
WriteAllText, WriteAllLines, WriteAllBytes, Create, and CreateText replace a file that already exists, and Delete does not use the Recycle Bin. Practise in a temp folder, and build paths with Path.Combine.
How to Call a File Method
Every File method is static, so you call it on the class name. Add using System.IO; at the top of the file:
string path = Path.Combine(Path.GetTempPath(), "codetofun-demo.txt");
File.WriteAllText(path, "hello"); // create or overwrite
File.AppendAllText(path, " world"); // add to the end
string text = File.ReadAllText(path); // "hello world"
bool there = File.Exists(path); // true
File.Copy(path, path + ".bak", true); // copy, allow overwrite
File.Delete(path + ".bak"); // remove the copy
File.Delete(path); // remove the file C# File Methods Table
All method guides in this C# tutorial series (37 tutorials), grouped by purpose.
Check / copy / move / delete
5 methodsAsk if a file is there, copy it, move or rename it, swap it safely, or remove it. Delete is quiet when the file is already gone.
| Method | Description | Tutorial |
|---|---|---|
Exists() | Check whether a file is there. Returns true or false and never throws. | Try it |
Copy() | Copy a file to a new path, with an option to overwrite an existing file. | Try it |
Move() | Move or rename a file. The original path is gone afterwards. | Try it |
Replace() | Swap a file in for another one and keep an optional backup of the old content. | Try it |
Delete() | Remove a file from disk. No error if the file is already gone. | Try it |
Create / open streams
7 methodsGet a FileStream, StreamReader, or StreamWriter for step-by-step work. Always wrap the result in a using block so the file is closed.
| Method | Description | Tutorial |
|---|---|---|
Create() | Create or overwrite a file and get a FileStream to write bytes into it. | Try it |
CreateText() | Create or overwrite a text file and get a StreamWriter for writing lines. | Try it |
Open() | Open a file as a FileStream with your choice of mode, access, and sharing. | Try it |
OpenRead() | Open an existing file for reading only and get a FileStream. | Try it |
OpenWrite() | Open a file for writing without emptying it, creating it if needed. | Try it |
OpenText() | Open an existing text file and get a StreamReader to read it line by line. | Try it |
AppendText() | Open a StreamWriter that adds text to the end of a file, creating it if needed. | Try it |
Read a whole file
4 methodsLoad text, lines, or raw bytes in one call. ReadLines is the lazy one: it hands you a line at a time, which suits big files.
| Method | Description | Tutorial |
|---|---|---|
ReadAllText() | Read a whole text file into one string. | Try it |
ReadAllLines() | Read every line of a text file into a string array. | Try it |
ReadAllBytes() | Read the raw bytes of a whole file into a byte array. | Try it |
ReadLines() | Read the lines of a text file lazily, one at a time, with IEnumerable. | Try it |
Write / append a whole file
5 methodsSave text, lines, or bytes in one call. WriteAll methods create or overwrite; AppendAll methods add to the end.
| Method | Description | Tutorial |
|---|---|---|
WriteAllText() | Save a string to a file in one call, creating it or replacing it. | Try it |
WriteAllLines() | Write an array or sequence of strings as lines, replacing the file. | Try it |
WriteAllBytes() | Save a byte array to a file, creating it or replacing it. | Try it |
AppendAllText() | Add a string to the end of a file, creating the file if it is missing. | Try it |
AppendAllLines() | Add lines to the end of a file, each followed by a line break. | Try it |
Attributes / security
4 methodsRead and change flags such as ReadOnly, and use NTFS encryption on Windows. Attributes are flags, so combine them with | and remove them with & ~.
| Method | Description | Tutorial |
|---|---|---|
GetAttributes() | Read the FileAttributes flags of a file or folder, such as ReadOnly and Directory. | Try it |
SetAttributes() | Change the FileAttributes flags of a file, such as turning ReadOnly on or off. | Try it |
Encrypt() | Encrypt a file with NTFS EFS so only the current Windows account can read it. | Try it |
Decrypt() | Remove NTFS EFS encryption from a file so any account can read it (Windows only). | Try it |
Timestamps
12 methodsRead or change when a file was created, last read, or last written. Each has a local-time form and a Utc form.
| Method | Description | Tutorial |
|---|---|---|
GetCreationTime() | Read when a file was created, as a local DateTime. | Try it |
GetCreationTimeUtc() | Read when a file was created, as UTC time that does not depend on the time zone. | Try it |
SetCreationTime() | Change the creation time of a file using a local DateTime. | Try it |
SetCreationTimeUtc() | Change the creation time of a file using a UTC DateTime. | Try it |
GetLastAccessTime() | Read when a file was last opened or read, as a local DateTime. | Try it |
GetLastAccessTimeUtc() | Read when a file was last opened or read, as UTC time that does not depend on the time zone. | Try it |
SetLastAccessTime() | Change the last access time of a file using a local DateTime. | Try it |
SetLastAccessTimeUtc() | Change the last access time of a file using a UTC DateTime. | Try it |
GetLastWriteTime() | Read when a file was last changed, as a local DateTime. | Try it |
GetLastWriteTimeUtc() | Read when a file was last changed, as UTC time that does not depend on the time zone. | Try it |
SetLastWriteTime() | Change the last write time of a file using a local DateTime. | Try it |
SetLastWriteTimeUtc() | Change the last write time of a file using a UTC DateTime. | Try it |
Frequently Asked Questions
Start with WriteAllText()
Save your first text file in one safe call, then read it back.
12 people found this page helpful
