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?
The exception means that the 3rd party is not comfortable with you reading while they are writing. Your first bet is to check if you can configure the 3rd party process to allow other processes to read while they write, but be aware that reading from a file that is being written to can be troublesome.
IOException Example. Here is simple example for IOException: When you run the above program where there is no file present in the file system, you would get the following exception: This is one of the IOException, but depends on the different problems in reading the file, your application may throw some other IOExceptions.
IOException is a checked exception and application developer has to handle in correct way. IOException has many sub classes that are specific in nature. That means, when your application searching to read a file, if the file is not found that there is a ileNotFoundException to be thrown. FileNotFoundException is a subclass of IOException.
When is IOException thrown Java application needs to handle failures related to reading, writing, and searching a file or a directory. java.io.IOException is the base exception class used for handling the failures. In a method of a class, try, catch, and finally block handles the exception.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With