---
title: "How can you read a file while it is being written to by another process?"  
description: "How can you read a file while it is being written to by another process?"  
author: "ICSM Computer"  
published: 2025-05-12  
updated: 2025-05-12  
canonical: https://www.mindstick.com/interview/34106/how-can-you-read-a-file-while-it-is-being-written-to-by-another-process  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How can you read a file while it is being written to by another process?

To **read a file while it is being written to by another process** in C#, you need to open it with **shared read access**. This lets your code read from the file even if another process has it open for writing — assuming that process allows sharing.

### Example: Open file with shared access

```cs
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string path = @"C:\Logs\active.log";

        try
        {
            using (var stream = new FileStream(
                path,
                FileMode.Open,
                FileAccess.Read,
                FileShare.ReadWrite)) // <-- allow reading even if another process is writing
            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                string content = reader.ReadToEnd();
                Console.WriteLine(content);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error reading file: " + ex.Message);
        }
    }
}
```

### Key Points:

| Option | Meaning |
| --- | --- |
| `FileAccess.Read` | You're only reading the file |
| `FileShare.ReadWrite` | Allows other processes to read/write while you read |
| `FileMode.Open` | Open the file (throws if it doesn't exist) |

> This works **only if the other process opens the file with a compatible sharing mode**.

### Tip for reading tailing logs:

If the file is being appended to (like a log), you can use a `while` loop to poll for new content, similar to `tail -f`.

## Answers

### Answer by ICSM Computer

To **read a file while it is being written to by another process** in C#, you need to open it with **shared read access**. This lets your code read from the file even if another process has it open for writing — assuming that process allows sharing.

### Example: Open file with shared access

```cs
using System;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        string path = @"C:\Logs\active.log";

        try
        {
            using (var stream = new FileStream(
                path,
                FileMode.Open,
                FileAccess.Read,
                FileShare.ReadWrite)) // <-- allow reading even if another process is writing
            using (var reader = new StreamReader(stream, Encoding.UTF8))
            {
                string content = reader.ReadToEnd();
                Console.WriteLine(content);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error reading file: " + ex.Message);
        }
    }
}
```

### Key Points:

| Option | Meaning |
| --- | --- |
| `FileAccess.Read` | You're only reading the file |
| `FileShare.ReadWrite` | Allows other processes to read/write while you read |
| `FileMode.Open` | Open the file (throws if it doesn't exist) |

> This works **only if the other process opens the file with a compatible sharing mode**.

### Tip for reading tailing logs:

If the file is being appended to (like a log), you can use a `while` loop to poll for new content, similar to `tail -f`.


---

Original Source: https://www.mindstick.com/interview/34106/how-can-you-read-a-file-while-it-is-being-written-to-by-another-process

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
