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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:30:56+00:00 2026-05-17T15:30:56+00:00

Hopefully more of a What am I doing wrong? than How do I? but

  • 0

Hopefully more of a “What am I doing wrong?” than “How do I?” but there you go…

Basically, I’m trying to get a program to behave in the following way:

  • First instance of the program opens up on PC1, opens a file for Read/Write access and then acts as the master program, doing a bunch of work on some other files I don’t want a whole bunch of users accessing at once.

  • Second instance of the program starts up, tries to open the file for Read/Write access, fails, enters Slave mode, opens the file for Read access, and periodically reads the status of the other files from this file.

That’s the plan, anyway. If anyone can suggest a better way to handle the master/slave decision-making, I’m open to suggestions.

Anyway, as a lead-up to implementation, I’ve got two instances of Visual Studio open. One is running a project called “GetFile”, the other is running a project called “TryGetFile”.

“GetFile” has a Private myStream As IO.FileStream object and opens the test file using this line:

myStream = IO.File.Open("\\[network path]\test.txt", IO.FileMode.OpenOrCreate,
                            IO.FileAccess.ReadWrite, IO.FileShare.ReadWrite)

This works fine and, as far as I can tell, should leave this file accessible for further Read/Write access by any other process, which is fine for this stage of the testing.

“TryGetFile” also has a Private myStream As IO.FileStream object, but it attempts the following open code:

myStream = IO.File.Open("\\[network path]\test.txt", IO.FileMode.Open, 
                            IO.FileAccess.Read, IO.FileShare.Read)

This doesn’t work at all. I get an IOException, which reports that test.txt is opened by another process and I’m not allowed to play with it.

Basically, I don’t see what the problem is; I don’t think “TryGetFile” is asking for any file access which “GetFile” forbids it from having. As far as I can see, “GetFile” shouldn’t be forbidding any kind of access at all…

What am I screwing up, here?

EDIT: Hmm…

Okay, Henk and Richard have answered the original question, as posed, pointing out that the problem is that “TryGetFile” is attempting to narrow the file sharing permission to Read, where “GetFile” already allows ReadWrite access. Altering “TryGetFile” to also allow ReadWrite sharing lets the code run.

Unfortunately altering the code as per their suggestions, so that both “GetFile” and “TryGetFile” permit IO.FileShare.Read sharing:

'GetFile
myStream = IO.File.Open("\\[network path]\test.txt", IO.FileMode.Open,
                            IO.FileAccess.ReadWrite, IO.FileShare.Read)

'TryGetFile
myStream = IO.File.Open("\\[network path]\test.txt", IO.FileMode.Open,
                            IO.FileAccess.Read, IO.FileShare.Read)

causes TryGetFile to throw an IOException again.

What’s wrong, here?

  • 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-17T15:30:57+00:00Added an answer on May 17, 2026 at 3:30 pm

    You should be able to do it this way, in all instances:

    1. Open with FileAccess.ReadWrite and FileShare.Read
    2. If #1 has failed open with FileAccess.Read and FileShare.ReadWrite

    The first will only work if no-one else has a write lock (and adds a write lock), but will allow other readers (which is then used in 2). But, as it turns out those subsequent readers must also allow others to write.


    Updated with that last note after testing in PowerShell ($name includes the absolute path of the file, to avoid the effect of PowerShell not directly surfacing the process’s underlying current directory):

    First process:

    $file = [io.file]::Open($name, [io.filemode]::Open, [io.fileaccess]::ReadWrite, [io.fileshare]::Read)
    

    Second process:

    $file = [io.file]::Open($name, [io.filemode]::Open, [io.fileaccess]::Read, [io.fileshare]::readwrite)
    

    And I see, in Process Explorer that the file is open in both processes.

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

Sidebar

Related Questions

I've been trying to get the XMLDSIG support in .NET to behave properly, more
I was doing this in a manual way, but i was thinking there may
Hopefully the answer is no, but are there any problems with using Request.Params instead
To begin with, I think this is a longshot but hopefully there is someone
If the title is confusing, hopefully this makes it more clear what I'm trying
Hopefully this is just a stupid mistake, but I can't see it. ; OK
Hopefully the good people at Stackoverflow can help me today - I basically drank
Hopefully someone can help me. I'm attempting to do the following: If a user
Hopefully someone here can help me! I'm trying to set up fancybox so when
Hopefully no one has asked this question - didn't see it, but I apologize

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.