C# File Methods

Beginner
7 min read
Updated: Nov 2026
37 method guides
Method index

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().

Overwrite and delete have no undo.

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:

C#
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 methods

Ask 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.

MethodDescriptionTutorial
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 methods

Get a FileStream, StreamReader, or StreamWriter for step-by-step work. Always wrap the result in a using block so the file is closed.

MethodDescriptionTutorial
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 methods

Load 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.

MethodDescriptionTutorial
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 methods

Save text, lines, or bytes in one call. WriteAll methods create or overwrite; AppendAll methods add to the end.

MethodDescriptionTutorial
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 methods

Read and change flags such as ReadOnly, and use NTFS encryption on Windows. Attributes are flags, so combine them with | and remove them with & ~.

MethodDescriptionTutorial
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 methods

Read or change when a file was created, last read, or last written. Each has a local-time form and a Utc form.

MethodDescriptionTutorial
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

They are the static methods of System.IO.File. They check, copy, move, and delete files, open streams, read and write whole files in one call, work with attributes and encryption, and read or change the created, last access, and last write times.
This index lists 37 guides: Exists, Copy, Move, Replace, Delete, the Create and Open family, the ReadAll, ReadLines, WriteAll, and AppendAll methods, GetAttributes, SetAttributes, Encrypt, Decrypt, and all twelve timestamp methods.
File.ReadAllText for one string, File.ReadAllLines for a string array, and File.ReadAllBytes for raw bytes. For a very large file use File.ReadLines, which gives you one line at a time instead of loading everything.
Yes. Every example works inside a demo folder in Path.GetTempPath(), creates the files it needs, and deletes its own folder at the end. Output never prints machine-specific absolute paths.
Read the C# File hub first. Then open WriteAllText(), ReadAllText(), Exists(), and Delete() from the tables below.

Start with WriteAllText()

Save your first text file in one safe call, then read it back.

WriteAllText() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful