Reading a file without an IOException in a third-party program

I am reading a file that is periodically written by a third-party program. Im using the following FileStream:

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

However, a third-party program writes the file, but uses FileShare.None

Therefore, sometimes I have a file for reading when a third-party program tries to open a file with exclusive access, but it does not work.

How can I read this file without causing problems for a third-party program? I would not mind letting go of my reading to give priority to another application. Any way to do this?

+6
source share
3 answers

What you want to do is to keep track of notifications about the creation of the process, and when you see that a particular process will occur, you will want to get rid of it.

IWbemServices :: ExecNotificationQuery

The following are some additional MSDN information. Here is also CodeProject: Process Information and Notifications Using WMI

+1
source

If this third-party process (.exe, Windows service, web application, etc.) ever stops, you can access the file when it happens, if it never stops or it is impossible to tell when it will work, you can do copy this file as soon as you turn on the server.

This will be valid or independent of the nature of your application, as well as how often you need to read this file.

+1
source

While I have not used it, have you tried using the System.IO.Filestream.Unlock method to help you transfer parts of the file that are not currently locked by other software? I just assume that other software is not blocking the whole file.

+1
source

Source: https://habr.com/ru/post/951038/


All Articles