Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:12:38+00:00 2026-06-13T16:12:38+00:00

I have following code to read a file StreamReader str = new StreamReader(File.Open(fileName, FileMode.Open,

  • 0

I have following code to read a file

StreamReader str = new StreamReader(File.Open(fileName, FileMode.Open, FileAccess.Read));
string fichier = str.ReadToEnd();
str.Close();

This is part of a asp.net webservice and has been working fine for an year now in production. Now with increasing load on server, customer has started getting “File already in use” error. That file is being read from this code and is never written to from application.

One problem that I clearly see is that we are not caching the contents of file for future use. We will do that. But I need to understand why and how we are getting this issue.

Is it because of multiple threads trying to read the file? I read that StreamReader is not thread safe but why should it be a problem when I am opening file in Read mode?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:12:39+00:00Added an answer on June 13, 2026 at 4:12 pm

    You need to open the file with read access allowed. Use this overload of File.Open to specify a file sharing mode. You can use FileShare.Read to allow read access to this file.

    Anothr possible solution is to load this file once into memory in a static constructor of a class and then store the contents in a static read-only variable. Since a static constructor is guaranteed to run only once and is thread-safe, you don’t have to do anything special to make it work.

    If you never change the contents in memory, you won’t even need to lock when you access the data. If you do change the contents, you need to first clone this data every time when you’re about to change it but then again, you don’t need a lock for the clone operation since your actual original data never changes.

    For example:

    public static class FileData
    {
        private static readonly string s_sFileData;
    
        static FileData ()
        {
            s_sFileData = ...; // read file data here using your code
        }
    
        public static string Contents
        {
            get
            {
                return ( string.Copy ( s_sFileData ) );
            }
        }
    }
    

    This encapsulates your data and gives you read-only access to it.

    You only need String.Copy() if your code may modify the file contents – this is just a precaution to force creating a new string instance to protect the original string. Since string is immutable, this is only necessary if your code uses string pointers – I only added this bit because I ran into an issue with a similar variable in my own code just last week where I used pointers to cached data. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to read a text file. const string FILENAME =
I have the following basic code to read a text file from a StreamReader:
I have following code : // Read properties file. Properties properties = new Properties();
I have the following code to read contents from a file to open multiple
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have the following code: f = open(path, 'r') html = f.read() # no
I have the following code to read a file in the root directory of
I have the following code trying to read a properties file: Properties prop =
I have the following code that read input from txt file as follow Paris,Juli,5,3,6
So I have the following code where I should read a Text File (This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.