Question

The following process remains waiting, while the file is present. I probably made ​​an error, but I don't see where.

System.IO.WaitForChangedResult result;
seeTransFile.WaitForChanged(System.IO.WatcherChangeTypes.Created);
string seeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
System.IO.FileSystemWatcher watcher = new FileSystemWatcher(SynDir, fileName + @".md5");
result = watcher.WaitForChanged(System.IO.WatcherChangeTypes.Created);

Can you help me?

Was it helpful?

Solution

FileSystemWatcher will only trigger when the file is created/changed. Existing files are ignored.

First start your FileSystemWatcher, then use Directory.GetFiles to get existing files.

If you need to read the contents of files detected by FileSystemWatcher, I recommend you verify that the owner of the file has released all locks on it by using this code:

try 
{
    // Attempts to open then close the file in RW mode, denying other users to place any locks.
    FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    fs.Close();
    fileClosed = true; // success
}
catch (IOException) {}

If a lock is present on the file, either wait for it to be released by its owner or put the file on a queue and retry later.

OTHER TIPS

since you already added FileSystemWatcher component configure it to watch as below. remove other code lines

System.IO.WaitForChangedResult result;
seeTransFile.Path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
seeTransFile.Filter = fileName + @".md5";
result = seeTransFile.WaitForChanged(System.IO.WatcherChangeTypes.Created);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top