0

I have 2 windows services. First windows service will compare 2 text files and it will generate a comparison result file. Once result file is created, FileSystemWatcher will catch the file create event and trigger the second windows service. Then second windows service start to read the content of the result file. There is a scenario that comparison result file is created but its' content still writing to the file. Because of that, second windows service give me an error that file is consuming by another process. To prevent that I used the following method.

private FileContent GetFileContent(string path)
{
        FileContent f = null;
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(FileContent));
            FileStream fileStream = new FileStream(path, FileMode.Open);
            f =  (FileContent)serializer.Deserialize(fileStream);
            fileStream.Dispose();

        }
        catch (Exception ex)
        {
           f = GetFileContent(path);
        }
        return f;
}

Besides this method, is there any way to wait until the file is released its' current consuming process then read it?

NoughT
  • 675
  • 4
  • 20
  • 39

0 Answers0