Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a file without causing IOException on 3rd party program

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?

like image 270
CathalMF Avatar asked Aug 05 '13 13:08

CathalMF


People also ask

Why can't I read from 3rd party files while they are writing?

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.

What is an example of an IOException?

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.

What is the difference between IOException and filenotfoundexception?

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 in Java?

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.


2 Answers

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

like image 61
josh poley Avatar answered Sep 28 '22 02:09

josh poley


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.

like image 40
Felipe Pereira Avatar answered Sep 28 '22 03:09

Felipe Pereira