C# File Operations, C# Input Output (C# I/O)
A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.
Reading and writing to files is often called file input and output, or file I/O for short.
File I/O in C# is fairly easy to do, compared to other languages like C++ and C. Everything you do will start with the File class.
When you're doing file I/O, you will likely need to take your data and turn it into a format that works for one of the output methods, and then parse the text coming back in, so that you can reconstruct your objects. In other words, there's more to the typical file writing process than just dumping stuff into a file—you'll also need to get your data into the right format.
You can also read and write text-based and binary files in a different way (a little at a time, instead of all at once). For either way, you start with File.OpenRead or File.OpenWrite, wrap the returned FileStream in a TextReader, TextWriter, BinaryReader, or BinaryWriter, depending on what you want. Then go crazy reading or writing. When you're done writing, it is a good idea to call the writer's Flush if you are writing, and call the reader or writer's Close method.
Required Namespaces to Enable c# File Operations
using System;
using System.IO;
How to Write File in C#?
C# Write File with TextWriter
TextWriter tw = new StreamWriter("date.txt");
tw.WriteLine(DateTime.Now);
tw.Close();
Writing to File with StreamWriter in C#
StreamWriter writer = new StreamWriter("c:\\test.txt");
writer.WriteLine("File created using StreamWriter class.");
writer.Close();
C# Flush String to File
File.WriteAllText("C:\\test.txt", "How C#");
Write String Array to File in C#
string[] howcsharp = new string[]
{
"c#",
"technology",
"comparison",
"algorithm",
"discussion",
"tutorial",
"faq"
};
File.WriteAllLines("file.txt", howcsharp);
C# File operations with FileStream
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
for (int i = 1; i <= 20; i++)
{
F.WriteByte((byte)i);
}
F.Position = 0;
for (int i = 0; i <= 20; i++)
{
Console.Write(F.ReadByte() + " ");
}
F.Close();
Using BinaryWriter to write Binary File in C#
int[] integerList = new int[] { 1, 4, 6, 7, 11, 55, 777, 23, 266, 44, 82, 93 };
using (BinaryWriter b = new BinaryWriter(File.Open("file.bin",
FileMode.Create)))
{
foreach (int i in integerList)
{
b.Write(i);
}
}
How to Read From File in C#
C# Read From File with TextWriter
Textreader tr = new StreamReader("date.txt");
Console.WriteLine(tr.ReadLine());
tr.Close();
Reading From File with StreamReader in C#
using (StreamReader sr = File.OpenText("path"))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
Read Everything From File with ReadAllText Function in C#
string file = File.ReadAllText("C:\\file.txt");
Console.WriteLine(file);
And here is the code to return file content to a list in C#:
List<string> fileLines = File.ReadAllLines("file.txt").ToList();
Counting number of lines of a file in C#:
int lineCount = File.ReadAllLines("file.txt").Length;
Example LINQ Queries with File Operations:
bool exists = (from line in File.ReadAllLines("file.txt") where
line == "Some line match" select line).Count() > 0;
Reading File Content to A String Array in C#
string[] lines = File.ReadAllLines("file.txt");
foreach (string line in lines)
{
if (line.Length > 80)
{
Console.WriteLine(line);
}
}
Getting/Printing List of Directories in C#
string[] dirs = Directory.GetDirectories("c:\\folder\");
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
Getting/Printing List of Files in a Folder in C#
string[] files = Directory.GetFiles("c:\\folder\");
foreach (string file in files)
{
Console.WriteLine(file);
}
Binary File Operations, Using BinaryReader in C# to Read Binary Files
using (BinaryReader b = new BinaryReader(File.Open("file.bin",
FileMode.Open)))
{
int pos = 0;
int length = (int) b.BaseStream.Length;
while (pos < length)
{
int v = b.ReadInt32();
Console.WriteLine(v);
pos += sizeof(int);
}
}