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

  • Home
  • SEARCH
  • 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 6742021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:47:37+00:00 2026-05-26T11:47:37+00:00

I need to be able to open a file to read it, maintaining a

  • 0

I need to be able to open a file to read it, maintaining a lock that denies other instances of the same application write access until I have written the amended file back to disk. The file is in a shared location on a network, and the app instances can be on any machine on the network.

I have tried using a FileStream with FileAccess.ReadWrite and FileShare.Read, with a Streamreader to read and then a StreamWriter (on the same FileStream) to write, but this corrupts the file. Other permutations of the FileAccess and FileShare don’t seem to solve my basic problem either.

So I tried closing the StreamReader before opening the StreamWriter, but that changes the CanRead and CanWrite properties of the FileStream, so I still can’t write.

Clearly I am taking the wrong approach, so can someone tell me how I should be approaching this? It seems a common enough thing to want to do – edit a file and block write access until the edited file is saved.

  • 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-05-26T11:47:38+00:00Added an answer on May 26, 2026 at 11:47 am

    If you want to write to a file, you need to take exclusive file access, otherwise other programs can read partially written data (your writes aren’t atomic). There are solutions to this, but they are quite complex.

    A solution could be something like this:

    static bool Read(FileStream fs, byte[] data, long position)
    {
        fs.Seek(position, SeekOrigin.Begin);
    
        if (fs.ReadByte() == 0)
        {
            // Block of data not finished writing
            return false;
        }
    
        fs.Read(data, 0, data.Length);
        return true;
    }
    
    static bool Write(FileStream fs, byte[] data, long position)
    {
        try
        {
            fs.Lock(position, data.Length + 1);
            fs.Seek(position, SeekOrigin.Begin);
            fs.WriteByte(0);
            fs.Write(data, 0, data.Length);
            fs.Seek(position, SeekOrigin.Begin);
            fs.WriteByte(1);
            fs.Unlock(position, data.Length + 1);
            return true;
        }
        catch (IOException)
        {
            return false;
        }
    }
    
    static bool Append(FileStream fs, byte[] data)
    {
        return Write(fs, data, fs.Length);
    }
    

    where you always keep open the FileStream as

    FileStream fs1 = new FileStream("Test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
    

    Before the data there is a “guard byte” that tells if the data is being written. If it’s being written then reads on it will fail. The file is locked “where it’s needed” using FileStream.Lock.

    This clearly works better with binary fixed-length data. If you have variable length data (or you need to atomically update more “regions” of a file) then it becomes more complex. Normally you use DBs for this reason 🙂

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

Sidebar

Related Questions

I need to be able to open a document using its default application in
I am working on a web app that needs read/write access to an SQLite
For an application I'm creating, I need to be able to read AAC and
I have a need to be able to open a file on disk but
I need to be able to open up an external URL in my website
I need to be able to load the entire contents of a text file
In Python I open a temporary file for write by using tempfile.mkstemp in order
I have a Windows Forms app that is able to open up a console
I need to make a file truly read only - to keep it as
I want to open a file for reading, the C++ way. I need to

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.