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?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • 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. – Bearcat9425 Aug 05 '13 at 13:47
  • 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. – CathalMF Aug 05 '13 at 13:53
  • 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 :D – aladd04 Aug 05 '13 at 13:59
  • 1
    The "Copy" function just opens the file, reads it and writes it to a new location. Essentially causing the same problem. – CathalMF Aug 05 '13 at 14:01
  • 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. – Bearcat9425 Aug 05 '13 at 14:02
  • 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. – CathalMF Aug 05 '13 at 14:07
  • 3
    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. – Bearcat9425 Aug 05 '13 at 14:13
  • 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. – aladd04 Aug 05 '13 at 14:24
  • 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. – Bearcat9425 Aug 05 '13 at 14:41

3 Answers3

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

josh poley
  • 7,236
  • 1
  • 25
  • 25
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.

Felipe Pereira
  • 11,410
  • 4
  • 41
  • 45
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.

Jamie Clayton
  • 1,007
  • 11
  • 24