i have two programs working on the same file i want the first program to check regularly on the file, and if any changes are made operate on the file and empty it. the other program writes to the file. the problem occurs when the second program tries to write on the file because it's used from the first one is there an (if there's any implemented C# program would be even better) algorithm to handle this?
问题:
回答1:
To check if file has changed you might first of all check its timestamp. In pseudo code, you might try something like this in the monitoring program:
try {
if(FileTimeStampHasChanged(FileName)) {
//Attempt to do something with the file
}
}
catch {
// Handle or ignore the exception if you can't open the file (maybe the other application hasn't finished writing). You can always retry later.
}
However, I would strongly recommend a different approach altogether. Here are the options I'd suggest.
- If you can alter the writing program, let it write to a temporary file (e.g. output.$$$) and let it rename it as soon as it's done writing. Renaming is instantaneous and doesn't cause any locking. This is, in my experience, one of the best simple solutions for concurrent access.
- If you cannot alter the writing program, then modify the reading one. Using the logic I described in point 1, let it try to rename the file to read and work on the renamed copy. If the rename fails, then it means that the writer isn't done, and an attempt can be done later.
- If you cannot alter any of the programs, then you can make your own "traffic warden" application to implement point 1. It requires more work, but it's still doable. Here's how:
- Configure the Writer to write into a folder, e.g. "Output".
- Configure the Reader to read from another folder, e.g. "Input".
- Write your Traffic Warden so that it monitors Output folder and, when the file changes, it moves it to Input folder. Moving is instantaneous as well (it's practically the same operation as renaming).
回答2:
You are looking for opening file in both programs with FileShare.Write or ReadWrite using FileStream constructor.
If you open file this way both will be able to write. Note that you will have to manually implement some synchronization mechanism between 2 programs so they don't overwrite each others data.
Closing file after each write and waiting for file becoming unlocked when opening it may be easier option depending on access pattern.
Note that if you don't have control over one of the programs your only option is to simply wait till it is done with file in your program (you can force other program to close file handle, but you just don't want to do that as it will likely corrupt data).