.NET
StreamReader
file handling
C# programming
file operations

How do I open an already opened file with a .net StreamReader?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

StreamReader can read a file that is already open only if the file was opened with a compatible sharing mode. If another process locked the file exclusively, your reader cannot bypass that lock. The usual solution is to open a FileStream yourself with the right FileShare value and then wrap it in StreamReader.

Open The File With Explicit Sharing

When you call new StreamReader(path), you do not directly control sharing behavior. For already-open files, it is clearer to build the FileStream explicitly and then pass that stream to the reader.

csharp
1using System;
2using System.IO;
3using System.Threading.Tasks;
4
5class Program
6{
7    static async Task Main()
8    {
9        await using var stream = new FileStream(
10            "application.log",
11            FileMode.Open,
12            FileAccess.Read,
13            FileShare.ReadWrite);
14
15        using var reader = new StreamReader(stream);
16        string content = await reader.ReadToEndAsync();
17        Console.WriteLine(content);
18    }
19}

FileShare.ReadWrite is common for log files because it allows another process to keep writing while your code reads.

Choose The Right FileShare Mode

Use FileShare.Read when other readers should be allowed but writers are not expected. Use FileShare.ReadWrite when a writer may still be appending while you read. Use FileShare.None only when you want exclusive access yourself.

csharp
1using System.IO;
2
3public static class SharedFileReader
4{
5    public static StreamReader Open(string path)
6    {
7        var stream = new FileStream(
8            path,
9            FileMode.Open,
10            FileAccess.Read,
11            FileShare.ReadWrite);
12
13        return new StreamReader(stream);
14    }
15}

Your side can request sharing, but it only succeeds if the original writer allowed compatible sharing when it opened the file.

When You Control Both Sides

If you own both the writer and the reader, design them together. Let the writer open the file with a sharing mode that permits reads, and let the reader request the matching mode. That avoids mysterious access errors and makes concurrent file access intentional instead of accidental.

For example, a logging process can write with FileShare.Read, while a monitoring process opens the same file for FileAccess.Read. That cooperative design is far more reliable than trying to recover from exclusive locks after deployment.

Reading A File That Is Still Growing

If another process is still appending to the file, ReadToEndAsync gives you a snapshot of the current contents. For log tailing, keep the stream open and continue reading as new lines appear.

csharp
1using System;
2using System.IO;
3using System.Threading.Tasks;
4
5class Program
6{
7    static async Task Main()
8    {
9        await using var stream = new FileStream(
10            "application.log",
11            FileMode.Open,
12            FileAccess.Read,
13            FileShare.ReadWrite);
14
15        using var reader = new StreamReader(stream);
16
17        while (true)
18        {
19            var line = await reader.ReadLineAsync();
20            if (line is null)
21            {
22                await Task.Delay(500);
23                continue;
24            }
25
26            Console.WriteLine(line);
27        }
28    }
29}

That pattern is a basic tail-style reader. In production, add cancellation, file-rotation handling, and error recovery.

Common Pitfalls

The most common misunderstanding is thinking that StreamReader itself solves file locking. The real control point is FileStream and its FileShare mode. Another frequent mistake is expecting FileShare.ReadWrite to override an exclusive lock created by another process. It cannot. If the first opener denied sharing, the second open still fails. Developers also get tripped up by assuming ReadToEndAsync will keep streaming new content forever. It will not. For live monitoring, keep the file open and poll for new data or use a proper log ingestion mechanism.

Summary

  • Open the file with FileStream when you need explicit sharing behavior.
  • Use FileShare.ReadWrite when another process may still be writing.
  • Shared access succeeds only if the original opener allowed it.
  • 'ReadToEndAsync reads a snapshot, not an endless live stream.'
  • Dispose the reader and stream correctly to avoid holding stale handles.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.