I have to create a Windows service which monitors a specified folder for new files and processes it and moves it to other location.
I started with using FileSyste
It is a little odd that you cannot use FileSystemWatcher
or presumably any of the Win32 APIs that do the same thing, but that is irrelevant at this point. The polling method might look like this.
public class WorseFileSystemWatcher : IDisposable
{
private ManaulResetEvent m_Stop = new ManaulResetEvent(false);
public event EventHandler Change;
public WorseFileSystemWatcher(TimeSpan pollingInterval)
{
var thread = new Thread(
() =>
{
while (!m_Stop.WaitOne(pollingInterval))
{
// Add your code to check for changes here.
if (/* change detected */)
{
if (Change != null)
{
Change(this, new EventArgs())
}
}
}
});
thread.Start();
}
public void Dispose()
{
m_Stop.Set();
}
}