---
title: "File I/O in C#"  
description: "In this blog, I’m explaining about File I/O in C#The .NET framework provides a group of classes and methods in the System.IO namespace to allow synchr"  
author: "priyanka kushwaha"  
published: 2015-03-12  
updated: 2018-02-22  
canonical: https://www.mindstick.com/blog/10834/file-i-o-in-c-sharp  
category: ".net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# File I/O in C#

In this blog, I’m explaining about File I/O in C#

The .NET framework provides a group of [classes and methods](https://www.mindstick.com/forum/160396/how-are-type-parameters-specified-and-utilized-in-generic-classes-and-methods) in the System.IO namespace to allow [synchronous and asynchronous](https://www.mindstick.com/forum/158327/explain-the-difference-between-synchronous-and-asynchronous-javascript-functions) reading from and writing to data streams and files. It includes following classes :

- Directory
- DirectoryInfo
- FileInfo
- [FileStream](https://www.mindstick.com/interview/34113/what-are-the-differences-between-filestream-flush-flushasync-and-dispose)

**1. Directory:** Contains [static methods](https://www.mindstick.com/forum/161339/static-methods-in-python) to create, move and enumerate directories and subdirectories. The [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp) of the directory class does a security check on all methods.

**2. DirectoryInfo:** Contains [instance methods](https://www.mindstick.com/forum/161894/what-s-the-difference-between-staticmethod-classmethod-and-instance-methods) to create, move and enumerate directories and subdirectories. If you want to reuse an object multiple times, use DirectoryInfo instead of Directory to avoid security checks.

**3. FileInfo:** Contains instance methods to create, copy, delete, move and open files and assists in the creation of FileStream objects. Use FileInfo if you want to reuse the File instance on a [single file](https://www.mindstick.com/forum/161633/how-do-you-merge-multiple-text-files-into-a-single-file).

**4. FileStream:** Provides implementations for the abstract stream class suitable for file-based streaming (random [file access](https://www.mindstick.com/forum/157674/what-are-file-types-and-file-access-in-operating-systems)). The FileStream class can open a file in [one of the two](https://answers.mindstick.com/qa/33135/name-one-of-the-two-players-who-has-played-for-liverpool-arsenal-and-chelsea-in-the-premier-league) modes either synchronously (read and write methods) or asynchronously (BeginRead and BeginWrite methods).

##### \

##### Example:

\

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace FileIOExample{    class Program    {        static void Main(string[] args)        {            //To create a directory            DirectoryInfo dir = new DirectoryInfo("c:\\FileIO");            //To create a sub directory            dir.CreateSubdirectory("Dotnet");            //To create a Sample.txt            FileInfo file = new FileInfo(@"c:\\FileIO\Dotnet\Sample.txt");            StreamWriter sw=file.CreateText();            sw.WriteLine("Sample file creation");            sw.Close();            StreamReader sr=file.OpenText();            Console.Write(sr.ReadToEnd());            if (dir.Exists)            {                DirectoryInfo[] dirs = dir.GetDirectories();                foreach (object dirname in dirs)                Console.WriteLine(dirname.ToString());            }            Console.ReadKey();        }    }}
```

##### \
Output:

Sample file creation

Dotnet

---

Original Source: https://www.mindstick.com/blog/10834/file-i-o-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
