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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:14:35+00:00 2026-05-13T18:14:35+00:00

I’m writing a C#.NET program which uses XmlSerializer to serialize and deserialize the project

  • 0

I’m writing a C#.NET program which uses XmlSerializer to serialize and deserialize the project the current user is working on to and from an XML file. This is working fine, but I’m trying to figure out a way to prevent two users from opening the same file off a network drive and having one user overwrite the previous user’s save. I essentially want the behavior that MS Word has, where if the program can not gain write access to a file when it is being opened it opens the file in a read only mode.

So how do I lock access to a file? Do I just leave a file stream object open and keep a reference to it? Do I continue to use the same filestream object for any intermediate saves rather then opening and closing them for each save? Or is there some other way to lock access to a file?

Also how do I ensure that the file is released even if my app crashes? Would this be a good case to implement IDisposable?

  • 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-13T18:14:35+00:00Added an answer on May 13, 2026 at 6:14 pm

    You don’t have to do anything special to make this work. First try to open the file with FileAccess.ReadWrite and FileShare.Read. If you don’t get an IOException, you’ll have exclusive write access to the file. Other processes can read from the file but can never gain write access.

    If that open attempt fails, you can assume that somebody else already has the file open for writing. Now switch your internal program logic to “read-only” mode and open the file with FileAccess.Read and FileShare.ReadWrite. If that still bombs then there’s something fundamentally wrong with the file, the IOException tells you what.

    Be very careful to avoid an “is the file locked” test. That can’t work reliably on a network, you have to keep the file opened after gaining access to it. As soon as you close it, another process can gain access to it, a nanosecond later.

    Another typical problem with this kind of locking is that the user typically wants to know who’s got a lock on the file. Windows doesn’t provide any standard way to find out. Office deals with this by creating a hidden file in the same directory with the same name as the file but another filename extension. It writes the name of the user in this file, available to any process that finds the file locked. You can do the same, Environment.UserName would be good for that.

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

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try setting the routeValues parameter with this overload: return RedirectToAction("Index",… May 14, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer What you're talking about is creating Metadata for your concrete… May 14, 2026 at 9:20 pm
  • Editorial Team
    Editorial Team added an answer You have to read the documentation more carefully, then two… May 14, 2026 at 9:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.