---
title: "How do you prevent a file from being modified while reading it?"  
description: "How do you prevent a file from being modified while reading it?"  
author: "ICSM Computer"  
published: 2025-05-15  
updated: 2025-05-15  
canonical: https://www.mindstick.com/interview/34122/how-do-you-prevent-a-file-from-being-modified-while-reading-it  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How do you prevent a file from being modified while reading it?

To **prevent a file from being modified while you are reading it** in C#, you need to open it with **exclusive read access** by using the appropriate `FileShare` mode.

### Use `FileShare.None` to Deny Others Access

When you open the file for reading, specify `FileShare.None` so that **no other process** can open the file (even for writing) while it's being read.

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

class FileReadLockExample
{
    public static void ReadWithExclusiveLock(string filePath)
    {
        using (FileStream stream = new FileStream(
            filePath,
            FileMode.Open,
            FileAccess.Read,
            FileShare.None)) // No sharing allowed
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string content = reader.ReadToEnd();
                Console.WriteLine(content);
            }
        }
    }
}
```

### `FileShare` Options Summary

| FileShare Option | Meaning |
| --- | --- |
| `None` | Denies all other access (exclusive lock). ✅ |
| `Read` | Allows others to read but not write. |
| `Write` | Allows others to write but not read. |
| `ReadWrite` | Allows others to read and write. |
| `Delete` | Allows others to delete the file. |

### Notes

1. **Exclusive locks** will throw an exception if another process tries to access the file.
2. If you're in a **multi-threaded app**, you can use a `lock` or `Mutex` in addition for thread-level synchronization.
3. In **real-time logging or shared file** scenarios, consider using `FileShare.ReadWrite` with retry logic instead.

## Read More

1. [What are the best practices to handle large log files in C#?](https://www.mindstick.com/forum/161607/what-are-the-best-practices-to-handle-large-log-files-in-c-sharp)
2. [What is the difference between File.Open, File.OpenRead, and File.OpenWrite?](https://www.mindstick.com/interview/34098/what-is-the-difference-between-file-open-file-openread-and-file-openwrite)
3. [Temporary file to be automatically deleted after the program ends?](https://www.mindstick.com/interview/34089/temporary-file-to-be-automatically-deleted-after-the-program-ends)

## Answers

### Answer by ICSM Computer

To **prevent a file from being modified while you are reading it** in C#, you need to open it with **exclusive read access** by using the appropriate `FileShare` mode.

### Use `FileShare.None` to Deny Others Access

When you open the file for reading, specify `FileShare.None` so that **no other process** can open the file (even for writing) while it's being read.

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

class FileReadLockExample
{
    public static void ReadWithExclusiveLock(string filePath)
    {
        using (FileStream stream = new FileStream(
            filePath,
            FileMode.Open,
            FileAccess.Read,
            FileShare.None)) // No sharing allowed
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string content = reader.ReadToEnd();
                Console.WriteLine(content);
            }
        }
    }
}
```

### `FileShare` Options Summary

| FileShare Option | Meaning |
| --- | --- |
| `None` | Denies all other access (exclusive lock). ✅ |
| `Read` | Allows others to read but not write. |
| `Write` | Allows others to write but not read. |
| `ReadWrite` | Allows others to read and write. |
| `Delete` | Allows others to delete the file. |

### Notes

1. **Exclusive locks** will throw an exception if another process tries to access the file.
2. If you're in a **multi-threaded app**, you can use a `lock` or `Mutex` in addition for thread-level synchronization.
3. In **real-time logging or shared file** scenarios, consider using `FileShare.ReadWrite` with retry logic instead.

## Read More

1. [What are the best practices to handle large log files in C#?](https://www.mindstick.com/forum/161607/what-are-the-best-practices-to-handle-large-log-files-in-c-sharp)
2. [What is the difference between File.Open, File.OpenRead, and File.OpenWrite?](https://www.mindstick.com/interview/34098/what-is-the-difference-between-file-open-file-openread-and-file-openwrite)
3. [Temporary file to be automatically deleted after the program ends?](https://www.mindstick.com/interview/34089/temporary-file-to-be-automatically-deleted-after-the-program-ends)


---

Original Source: https://www.mindstick.com/interview/34122/how-do-you-prevent-a-file-from-being-modified-while-reading-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
