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 8270229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:31:20+00:00 2026-06-08T06:31:20+00:00

for an application that uses a File as some sort of global storage for

  • 0

for an application that uses a File as some sort of global storage for device reservations in a firm I need a way to read and write to a file (or lock a file, read from it, write to it, and unlock it). A little code snippet will shot what I mean:

FileStream in = new FileStream("storage.bin", FileMode.Open);
//read the file
in.Close();

//!!!!!
//here is the critical section since between reading and writing, there shouldnt
//be a way for another process to access and lock the file, but there is the chance
//because the in stream is closed
//!!!!!
FileStream out = new FileStream("storage.bin", FileMode.Create);
//write data to file
out.Close();

this should get something like this

LockFile("storage.bin");
//read from it...
//OVERwrite it....
UnlockFile("storage.bin");

the method should be absolute safe, since the program should run on 2000 devices at the same time

  • 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-08T06:31:21+00:00Added an answer on June 8, 2026 at 6:31 am

    Simply holding a FileStream open with exclusive (not shared) access will prevent other processes from accessing the file. This is the default when opening a file for read/write access.

    You can ‘overwrite’ a file that you currently hold open by truncating it.

    So:

    using (var file = File.Open("storage.bin", FileMode.Open))
    {
        // read from the file
    
        file.SetLength(0); // truncate the file
    
        // write to the file
    }
    

    the method should be absolute safe, since the program should run on 2000 devices at the same time

    Depending on how often you’re writing to the file, this could become a chokepoint. You probably want to test this to see how scalable it is.

    In addition, if one of the processes tries to operate on the file at the same time as another one, an IOException will be thrown. There isn’t really a way to ‘wait’ on a file, so you probably want to coordinate file access in a more orderly fashion.

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

Sidebar

Related Questions

We have a web application that uses dropbox as file storage. We have currently
I'm building a application that uses a file to configure some fonts. It's like
We developed an application that uses Excel interop libraries (Microsoft.Office.Interop.Excel) to read some Excel
I have an ASP.NET application that uses a custom .NET library (a .DLL file).
I am debugging an application that uses an XML file to store data as
I wrote an application that uses GraphViz to generate some graphs as .gif's according
I am developing an application that uses javascript to perform some arithmetical calculations in
I have a MSDOS clipper application that uses dBase as it's database engine, some
I have a windowed Flex application (AIR) that uses an HTML file for the
Let say i have one asp.net application that is having some page that uses

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.