What you are typically trying to achieve is a monitoring tool to watch a folder.
To watch a file / folder from .Net , you can use the FileSystemWatcher class that is available under the System.IO namespace.
private void WatchFolder(string folderPath)
{
//Create a file system watcher.
System.IO.FileSystemWatcher fsWatcher = new FileSystemWatcher();
//set the file system watcher properties
fsWatcher.Path = folderPath;
//set the notify filter attributes.
fsWatcher.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
fsWatcher.NotifyFilter = fsWatcher.NotifyFilter | System.IO.NotifyFilters.FileName;
fsWatcher.NotifyFilter = fsWatcher.NotifyFilter | System.IO.NotifyFilters.Attributes;
//add event handlers for changed, and created events of the filesystemwatcher.
fsWatcher.Changed += new FileSystemEventHandler(fsEventOccured);
fsWatcher.Created += new FileSystemEventHandler(fsEventOccured);
//enable the FileSystemWatcher to monitor the folder by setting the EnableRaisingEvents property //to true.
try
{
fsWatcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
//
}
finally
{
//stop monitoring
fsWatcher.EnableRaisingEvents = false;
}
}
/// <summary>
/// FileSystem passes the event occured.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private void fsEventOccured(object sender, System.IO.FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case WatcherChangeTypes.Changed:
//e.FullPath gives you the file path in the folder.
break;
case WatcherChangeTypes.Created:
//file is created here.
break;
default:
break;
}
}
Loop thru the WatcherChangeTypes Enum for other actions that can occur on the folder.