Reading a file without causing IOException on 3rd party program
Asked Answered
A

3

6

I'm reading a file which is periodically being written to by a 3rd party program. Im reading using the below FileStream:

using (FileStream fs = new FileStream(
                         FullPath,
                         FileMode.Open,
                         FileAccess.Read,
                         FileShare.ReadWrite))

However the 3rd party program is writing to the file but is using the FileShare.None

So occasionally I've got the file open for reading when the 3rd party program tries to open the file with exclusive access but fails.

How can I read this file without causing problems for the 3rd party program? I wouldn't mind releasing my read to give priority to another application. Any way to do this?

Allotropy answered 5/8, 2013 at 13:36 Comment(9)
Have you tried to copy it to a temp location and read from there? Typically what I do is create a function that attempts to read a file if its in use it returns a false but if not returns a true then I attempt to copy and move the file to a location I can work with it. Working with a production file can be potentially dangerous, especially with 2 processes in the mix.Erinn
File.Copy just performs a FileStream in the background. The problem is not when I try to read and its locked. Its when I have access to the file and the 3rd party program tries to gain exclusive access but cant because I have it open.Allotropy
What if you copy the file to a temp location and let your program use that temp file so that way the original file is not in use and can therefore still be accessed by the 3rd party program with no problems. If that makes sense :DIsla
The "Copy" function just opens the file, reads it and writes it to a new location. Essentially causing the same problem.Allotropy
Yeah I mean a copy operation will be faster than a read and process operation, it sounds like logistically you are always going to have the potential for stepping on the other processes toes, at least if you are just copying your in and out faster and reduce that risk. What I would do is have a fileSystem watcher watch that file for modifications which means 3rd party just updated then attempt to read which will fail until 3rd party releases it, then perform copy and get out and work with temp file.Erinn
Yes your right it will help reduce the risk of it happening but it will still happen. I already loop around until I have access but the issue is the same. When I have access the other application tries to write again and fails.Allotropy
Well I am not sure you are going to be able to eliminate this problem given the way they are writing to the file. If you can't eliminate a problem next best thing is to try and reduce its chances of happening as much as possible.Erinn
Bearcat is correct. If the 3rd party program is requesting exclusive access vie FileShare.None, there's nothing (I know of) that you can do about it. You just have to find a workaround to reduce the chances of errors happening as much as possible and handle the errors that do occur in the best way possible for your situation. EDIT: Assuming you can't "pause" the 3rd party application so that it just catches up on the read/writes after you do what you need to have done with yours.Isla
Correct all this is assuming there is not a way to pause 3rd party programmatically to do your processing. Only other thing I can think of is if there is a predictable schedule to its writes and catching it during those times. Another thought I had was place a file watcher on that file and wait for modified times, that will tell you that the 3rd party just wrote to the wrote and the likiehood of it doing another write right away is low so as soon as its done do your copy and work with temp file. FileSystemWatcher will not lock the file.Erinn
P
1

What you want to do is monitor for process creation notifications, and when you see the specific process come into existence, you will want to get out of its way.

IWbemServices::ExecNotificationQuery

Here are some some additional details off of MSDN. Here is also a CodeProject: Process Information and Notifications using WMI

Psychrometer answered 17/8, 2013 at 22:9 Comment(1)
Unfortunately the other process is a service so is always running.Allotropy
D
1

If this 3rd party proccess (.exe, windows service, web app, etc.) ever stops, you could access the file when that happens, if it never stops or it's impossible to tell when it will run, you could do a copy this file as soon as you turn on the server.

This will be valid or not depending on the nature of your app and the other one, and on how often you need to read this file.

Ducks answered 17/8, 2013 at 22:59 Comment(1)
Maybe this service has some public method from where you can get the same info that is in the file.Ducks
B
1

While I haven't used it, have you tried use the System.IO.Filestream.Unlock method to help you stream parts of the file that are not currently locked by the other software? I'll just assume the other software isn't locking the whole file.

Birmingham answered 4/1, 2014 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.