Hi,
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?