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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:21:03+00:00 2026-06-11T02:21:03+00:00

I want to open file for append on Windows 7 host using C#/.NET. I

  • 0

I want to open file for append on Windows 7 host using C#/.NET. I want to use usual file output operations for this purpose. I found such solution:

FileStream trace_fd = new FileStream(r"c:\temp\testlog.txt", 
                                     FileMode.Append, 
                                     FileAccess.Write, 
                                     FileShare.ReadWrite)

My tracefile is readable even if my program with line above is up and running. I don’t have exception that file is used by another process anymore from proper code like this:

FileStream good_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                              FileMode.Read, 
                                              FileAccess.Read,
                                              FileShare.ReadWrite)

Then I have another program (buggy log scanner) with such line:

FileStream bad_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                             FileMode.Read, 
                                             FileAccess.Read, 
                                             FileShare.None)

Such line is obviously a bug for log scanner. If my program is not running then buggy logscaner will get exclusive lock and subsequent runs of my program will fail to get fd for trace file.

My question is what can I do to prevent such horrific scenario from happening.
E.g. can I set file permissions for the tracefile to prevent anyone from taking exclusive lock? If yes then how? Anything else which can protect my program from buggy log scanner problem? Note that I have to keep the same trace filename between my program runs.

  • 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-11T02:21:05+00:00Added an answer on June 11, 2026 at 2:21 am

    You can not set permissions to allow any access to a file, but somehow restrict FileShare.None as file sharing mode is not related to access permissions.

    Permissions give a process (based on account it runs under) access to a file/resource. If access granted process can open file with whatever share mode it desires. If this share mode does not conflict with existing share modes on the file request succeeds and now file will have this share mode (combined with previous one). See CreateFile- dwShareMode section for details.

    In your case log reader needs to have permissions to open file, so it will be able to set ShareMode.None if it the first process to open file. As devshorts says there is not much you can do short of changing offending process OR hacking file access methods (search for “hook CreateFile”).

    Side note: if your log reader runs under the same account as other processes permissions will not help for one additional reason – as there is no “per process” permissions in Windows and all processes will share the same user’s permissions.

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

Sidebar

Related Questions

I want to open a file from a Class in C# using a Process,
I want to open a file for reading. However, in the context of this
I want to open a text file, append a single line to it, then
I want to append some nodes to an xml document using Linq2XML. The file
I want to open file and read it that I pass from console. Like
What I want open a file if it exists, create it if it not
want to open pdf file when a user clicks on hyperlink shown in gridview
I want to open pdf file in webview but the scenario is different here,
Example: say I want to open a file. If I get a FileNotFoundException ,
I have a PHP page where i want to open a file on a

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.