C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# File I/O

C# File I/O

shape Description

C# File I/O, which allows file handling, is a very important feature for many web applications. Microsoft .NET Framework offers System.IO namespace, that provides various classes to enable the developers to do File I/O (Input / Output operations). C# programming language has the ability to write information to a file and read it back in. This is often called "file input and output", or simply "file I/O". Writing to files and reading from them is actually a very simple task in C#.

shape Streams

There are two main streams: the Input stream and the Output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).
  • StreamReader
  • StreamWriter

shape I/O Classes

Below are few of C# I/O Classes:
Class Description
File Modify Files
FileStream Read and write data in files
MemoryStream Random access of data from memory
StreamReader Byte stream character reading
StreamWriter Writing Character to stream
Directory Operation on Directories
BufferedStream Allow temporary storage of stream of bytes

shape Example

[csharp] using System; using System.IO; namespace SPLessons { class Program { static void Main() { using (FileStream stream = File.Open("D:\\splesson.txt", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(303); writer.Write(720); } using (FileStream stream = File.Open("D:\\splesson.txt", FileMode.Open)) using (BinaryReader reader = new BinaryReader(stream)) { int a = reader.ReadInt32(); int b = reader.ReadInt32(); Console.WriteLine(a); Console.WriteLine(b); } } } } [/csharp] Output:

shape Note