YouTip LogoYouTip

Csharp File Io

# C# File Input and Output A **file** is a collection of data stored on a disk with a specified name and directory path. When a file is opened for reading or writing, it becomes a **stream**. Fundamentally, a stream is a sequence of bytes transmitted through a communication channel. There are two main types of streams: **input streams** and **output streams**. An **input stream** is used to read data from a file (read operation), and an **output stream** is used to write data to a file (write operation). ## C# I/O Classes The System.IO namespace contains various classes for performing different file operations, such as creating and deleting files, reading or writing to files, closing files, etc. The following table lists some commonly used non-abstract classes in the System.IO namespace: | I/O Class | Description | | --- | --- | | BinaryReader | Reads primitive data from a binary stream. | | BinaryWriter | Writes primitive data in binary format. | | BufferedStream | Temporary storage for a byte stream. | | Directory | Helps in manipulating directory structures. | | DirectoryInfo | Used for performing operations on directories. | | DriveInfo | Provides information about drives. | | File | Helps in manipulating files. | | FileInfo | Used for performing operations on files. | | FileStream | Used for reading and writing to any location in a file. | | MemoryStream | Used for random access to data stored in memory. | | Path | Performs operations on path information. | | StreamReader | Used for reading characters from a byte stream. | | StreamWriter | Used for writing characters to a stream. | | StringReader | Used for reading from a string buffer. | | StringWriter | Used for writing to a string buffer. | ## The FileStream Class The **FileStream** class in the System.IO namespace helps with reading, writing, and closing files. This class is derived from the abstract class Stream. You need to create a **FileStream** object to create a new file or open an existing file. The syntax for creating a **FileStream** object is as follows: FileStream = new FileStream( ,, , ); For example, to create a FileStream object **F** to read a file named **sample.txt**: FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read); | Parameter | Description | | --- | --- | | FileMode | The **FileMode** enumeration defines various methods for opening files. The members of the FileMode enumeration are: * **Append**: Opens an existing file and places the cursor at the end of the file. If the file does not exist, it creates a new file. * **Create**: Creates a new file. If the file already exists, it deletes the old file and then creates a new file. * **CreateNew**: Specifies that the operating system should create a new file. If the file already exists, an exception is thrown. * **Open**: Opens an existing file. If the file does not exist, an exception is thrown. * **OpenOrCreate**: Specifies that the operating system should open a file if it exists; otherwise, a new file should be created with the specified name. * **Truncate**: Opens an existing file. Once opened, the file is truncated to zero bytes. We can then write new data to the file, but the original creation date of the file is retained. If the file does not exist, an exception is thrown. | | FileAccess | The members of the **FileAccess** enumeration are: **Read**, **ReadWrite**, and **Write**. | | FileShare | The members of the **FileShare** enumeration are: * **Inheritable**: Allows the file handle to be inherited by child processes. This is not directly supported by Win32. * **None**: Refuses sharing the current file. Any request to open the file (by this process or another process) will fail until the file is closed. * **Read**: Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file. * **ReadWrite**: Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file. * **Write**: Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may be required to access the file. * **Delete**: Allows subsequent deletion of the file. | ## Example The following program demonstrates the usage of the **FileStream** class: ## Example using System; using System.IO; namespace FileIOApplication { class Program { static void Main(string[] args) { 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(); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1 ## C# Advanced File Operations The example above demonstrates simple file operations in C#. However, to fully utilize the powerful features of the C# System.IO classes, you need to know the common properties and methods of these classes. In the following sections, we will discuss these classes and the operations they perform. Please click the links to learn more about each topic: | Topic | Description | | --- | --- | | (#) | It involves reading and writing to text files. The **StreamReader** and **StreamWriter** classes help in reading and writing text files. | | (#) | It involves reading and writing to binary files. The **BinaryReader** and **BinaryWriter** classes help in reading and writing binary files. | | (#) | It allows C# programmers to navigate and locate Windows files and directories. |
← Csharp Text FilesCsharp Exception Handling β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.