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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:23:50+00:00 2026-05-24T20:23:50+00:00

While writing some code dealing with logs and files, I’ve discovered some baffling behaviour

  • 0

While writing some code dealing with logs and files, I’ve discovered some baffling behaviour in windows file io. Does anyone know why this test would fail with “cannot read file” message?

[TestMethod]
public void SouldAllowReads()
{
    using (var file = File.Open(_path, FileMode.Create, FileAccess.Write, FileShare.Read))
    {
        using (var file2 = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            //works ok, doesn't throw
        }

        try
        {
            using (var file3 = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read))
            { 
                //fails
            }
        }
        catch (IOException)
        {
            Assert.Fail("cannot read file");
        }
    }
}

PS. _path = Path.GetTempFileName();

EDIT:

I’ll mark elevener answer as correct one, but there is one thing that bothers me in this design. .NET methods such as File.ReadAllText(_path) throw exceptions, which just shouldn’t happen.

For example this snipped my test would also fail assertion:

        try
        {
            string text = File.ReadAllText(_path);
        }
        catch (IOException)
        {
            Assert.Fail("cannot read file");
        }
  • 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-24T20:23:51+00:00Added an answer on May 24, 2026 at 8:23 pm

    You have var file = open with FileAccess.Write and at the same time are trying to open var file3 = with fileshare mode FileShare.Read that doesn’t allows concurrent write access.

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

Sidebar

Related Questions

While doing some casual reading I came across an interesting quote by Scott Meyers
I am writing a WPF application using C# and I need some help with
I want to write a server side code. It should work with popular browsers
I have an applet, which I was writing in Java. Recently, I thought that
Preface I'm learning about computer math by writing and refining my own BigInt library.
I'm writing my first socket project and I have a problem. When the client
I would like to use libev for a streaming server I am writing. This
I'm writing a bunch of data to a socket, however, after what seems like
I would like to remove an extra line at the front of the text
I seem to be in over my head. I have been trying to change

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.