0

I'm trying to have two applications communicate trough a simple text file. My problem is that I have a Windows Store App which writes like so:

byte[] data = Encoding.UTF8.GetBytes(toSend);
var folder = KnownFolders.PicturesLibrary;
var file = await folder.GetFileAsync("orders");

using (var stream = await file.OpenStreamForWriteAsync())
{
      await stream.WriteAsync(data, 0, data.Length);
}

The writing happens as expected and the line is in the format I need it. I have a FileSystemWatcher in the second app which is a simple console app. The watcher is set like this:

FileSystemWatcher watcher;            
watcher = new FileSystemWatcher(path);
watcher.Filter = "orders";
watcher.NotifyFilter = NotifyFilters.LastWrite;
var result = watcher.WaitForChanged(WatcherChangeTypes.Changed);

I tried to set the filter with size just as a test. If the event is called, I read the file like this:

private String readFile()
{
    FileStream fileStream = new FileStream(this.path + @"\orders",    FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader fileReader = new StreamReader(fileStream);
    String line = "";
    line = fileReader.ReadLine();
    return line;
}

So fairly simple usage I'd think. My problem is that the FileSystemWatcher catches the modification when my other app opens the text file therefore it reads the previous line intended.

Does anyone have an idea how to "wait" for the file to be written?

Thanks!

SOLVED Thanks to you guys. I tried looping by creating a small recursive function and I noticed that I need at most two loops for the correct line to be created so everything is fine. Thank you again.

Pierre P.
  • 890
  • 1
  • 18
  • 41
  • 2
    I see that there are some questions here about the same issue: http://stackoverflow.com/questions/7210381/filesystemwatcher-fires-before-file-is-saved-how-do-you-pause-the-process http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written – Amr Elgarhy Feb 10 '16 at 10:20
  • 1
    I think your best bet is to validate that the change was one you were looking for. Maybe save the last read line and see if it is indeed different on the next read. FileSystemWatcher events are quite plentiful, because the actual file operations are more complex than it first seems. – Jens Feb 10 '16 at 10:26
  • Thanks for the quick answers! AmrElgarhy, After watching the links, my problem being that I have to make sure the file has been changed I'll have to loop until it is done which I didnt really like in terms of performance. Jens, I already make sure that the new line is not the same but that means that the events triggered are late since I only have the previous ones. – Pierre P. Feb 10 '16 at 10:54
  • Well, while your watcher application regards read/write locks, the application writing the file does not seem to open the file with a lock. I guess you could easily solve your problem if you open the file with a lock for writing and then the monitor application makes sure to handle a locked file correctly. – Thorsten Dittmar Feb 10 '16 at 11:17

0 Answers0