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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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).
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:
However, I would strongly recommend a different approach altogether. Here are the options I'd suggest.