


This will keep waiting as long as the file exists and is inaccessible. A simple way to achieve that would be to change the WaitForFile() method into something like this: public static bool WaitForFile(FileInfo file) In addition to that, you need a way to confirm that the file actually exists. There's a bug in the WaitForFile() method, that is, it currently waits while the file is not locked (not the other way around). I am wondering if somehow one of the NotifyFilter comes into play here? As I further analyze the files in these folders, some of them are log files getting written in realtime, so it could be that the file is being written to for hours before it's actually completed. System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()Īny advice on this would be appreciated. System.IO.FileSystem.CopyFile(String sourceFullPath, Stringį(Object source, FileSystemEventArgs e) inĭevelopment\Source\repos\FolderWatchCG\FolderWatchCG\Program.cs:lineĦ1 at .c.b_139_1(Object System.IO.IOException: The process cannot accessīecause it is being used by another process. I've noted this Powershell version Copy File On Creation (once complete) and I'm not sure if the answer here could be pointing me in the right direction b/c I'm even less versed in PS than I am in C#.ĮDIT #1: I should have tested for longer before accepting the answer - I think we're close but after about a minute of the program running, I got the following error before the program crashed: I took the IsFileLocked and WaitForFile methods from other posts on SO, but I'm clearly not implementing them correctly. If I use the WaitForFile line, nothing ever happens. I don't seem to know what I'm doing with the WaitForFile(file) line - if I comment out that line and the file creation is instant, the file copies as intended.

What I'm attempting to do in the OnCreated method is to check and wait until the file is done being created, and then copy the file to another destination. TODO: Add some logic to make it async and cancelable This will lock the execution until the file is ready Public static void WaitForFile(FileInfo filename) or does not exist (has already been processed) Using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None)) Public static bool IsFileLocked(FileInfo file) String destFile = Path.Combine(destPath, createdFile) String createdFile = Path.GetFileName(wctName) Specify what is done when a file is created.įileInfo file = new FileInfo(e.FullPath) Private static void OnCreated(object source, FileSystemEventArgs e) Watcher.Created += new FileSystemEventHandler(OnCreated) String sourcePath = watcher = new FileSystemWatcher(sourcePath)
#FOLDERWATCH CODE#
I have gone through several SO posts and pieced together the following code trying to accomplish this: using System Now I have to handle the fact that most of the time, these files are large and take several minutes to complete creation. So far it's working for the basic functionality. txt files to another folder, using the same file name.
#FOLDERWATCH WINDOWS#
I built a console app that monitors a set of folders on a Windows 2019 Server and copies any newly-created.
