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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:57:35+00:00 2026-05-15T23:57:35+00:00

I am developing an application in C# (.NET), and am having trouble dealing with

  • 0

I am developing an application in C# (.NET), and am having trouble dealing with file locking.

  • My main application (A) needs read/write access to a certain file.
  • A separate application (B) needs read access to the same file.
  • I need to prevent the user from editing or deleting the file while my application (A) is running. The application (A) is long-running. The file must not be deleted while (A) is running, even when it is not actively being read from or written to.

I have full control of the source of (A) and (B), so I can modify either of them.

How can I stop a user from modifying/deleting a file while application (A) is running, while allowing application (A) to read/write, and application (B) to read?

  • 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-15T23:57:36+00:00Added an answer on May 15, 2026 at 11:57 pm

    Use FileShare.Read to only allow reads from other applications. You can lock the file by having a stream open while the application A runs. You need a NonClosingStreamWrapper to avoid disposing the stream when you dispose your StreamWriter (this happens automatically with using)

    NonClosingStreamWrapper by Jon Skeet can be found from here

    Example

    When application starts use this to lock the file

    FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
    

    When writing to a file use

    using (StreamWriter sr = new StreamWriter(new NonClosingStreamWrapper(fileStream)))
    {
        // File writing as usual
    }
    

    When application ends use this to release the file

    fileStream.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem with an ASP.NET MVC application that I'm developing. I'm still
I am developing an ASP.Net MVC 3 Web application and I am having some
I am developing a .NET application (using C#) that needs to massage some Adobe
I'm developing an application (winforms C# .NET 4.0) where I access a lookup functionality
I'm using vb.net and sql server 2000 for developing my application....The problem i'm having
I am developing a commercial VB.net WPF application that needs user generated scripts for
Hi I am developing a asp.net web application. I am having 3 drop down(date,
I'm developing a web application using MVC3 in VB.NET. I having difficulty setting a
Hi i'm having problems with this. I am developing an asp.net mvc 2 application.
We are developing a very large web application in .Net 3.5. Two separate vendors

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.