i currently am developing a program that monitors a folder, if anything new is added to the folder it is then copied to another folder.
the code i have so far is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace filemover
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string sourcePath = @"C:\Documents and Settings\chris.kennedy\Desktop\start";
string targetPath = @"C:\Documents and Settings\chris.kennedy\Desktop\finish";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
so far, this copies the file from the start folder to the finish folder.
how do i monitor the start folder and if there is something new, how do i get the code to recognise the new file.
it would not be appropriate to just overwrite the original files with the ones copied from the start folder as when this is operational, the amount of data being moved would be enough to severely slow down the computer/server