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

Give me your Email address and i'll email you a project that I made that monitors all files across your whole computer and tells you the information about each file

or you can look up how to use the filesystemwatcher

Here is a similar example:

private void StopWatcher()
    {
      if (_fsWatcher != null)
      {
        try
        {
          _fsWatcher.EnableRaisingEvents = false;
          _fsWatcher.Dispose();
          _fsWatcher = null;
        }
        catch { }
      }
    }
    /* -------------------------------------------------------------------- */
    private void StartWatcher(string ExeConfig)
    {
      StopWatcher();

      FileSystemWatcher _fsWatcher = new FileSystemWatcher();
      _fsWatcher.Path = Path.GetDirectoryName(ExeConfig);
      _fsWatcher.Filter = Path.GetFileName(ExeConfig);
      _fsWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
      _fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Created += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Deleted += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);
      _fsWatcher.EnableRaisingEvents = true;
    }
    /* -------------------------------------------------------------------- */
    private void OnChanged(object source, FileSystemEventArgs e)
    {
      ReloadConfig();
    }
    /* -------------------------------------------------------------------- */
    private void OnRenamed(object source, RenamedEventArgs e)
    {
      ReloadConfig();
    }
commented: Nice snippet +7

Scott, I wonder how you do it, to come up every time with these little crystal clear snippet gems! :)

Half of the code I have already :) I have miles and miles of code for applications i've done over the years.

hi,

i have tried couple of times but it didn't work well.Always FileSystemWacther raises duplicate events.

how to stop being raised events twice?

My sample coed:
public Class MyWatcher : System.ComponentModel.Component,ISupportInitialize
{
private System.ComponentModel.Container components = null;

FileSystemWatcher watcher = new FileSystemWatcher();
private void ReleaseEvents()
{
if (watcher != null)
{
watcher.Renamed -= new RenamedEventHandler(fileRenamed);
watcher.Deleted -= new FileSystemEventHandler(fileDeleted);
watcher.Changed -= new FileSystemEventHandler(fileChanged);
watcher.Created -= new FileSystemEventHandler(fileCreated);
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
// private bool disposed = false;

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (watcher != null)
{
ReleaseEvents();
watcher.Dispose();
}
if (components != null)
{
components.Dispose();
}


}

base.Dispose(disposing);

}
#region ISupportInitialize Members

public void BeginInit()
{

}
public void EndInit()
{
this.fullpath ="some Path";
FileWatcher();
}

string fullpath = null;
string FullPath
{
get { return fullpath; }
set { fullpath = value; FileWatcher();}//

}
public void FileWatcher()
{

if (fullpath != null)
{
watcher.Path = Path.GetDirectoryName(fullpath);
watcher.Filter = fileName;

watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName |NotifyFilters.DirectoryName ;

ReleaseEvents();

watcher.Renamed += new RenamedEventHandler(fileRenamed);
watcher.Deleted += new FileSystemEventHandler(fileDeleted);
watcher.Changed += new FileSystemEventHandler(fileChanged);
watcher.Created += new FileSystemEventHandler(fileCreated);


watcher.EnableRaisingEvents = true;

}

}

void fileChanged(object sender, FileSystemEventArgs e)
{

ReadMethod(e.FullPath);


}

void fileCreated(object sender, FileSystemEventArgs e)
{


ReadMethod(e.FullPath);


}
void fileDeleted(object sender, FileSystemEventArgs e)
{

}
void fileRenamed(object sender, RenamedEventArgs e)
{
ReadMethod(e.FullPath);

}
ArrayList alist= null; ---instance variable

void ReadMethod(string filename)
{

using (FileStream fs=new FileStream(filename,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite))
{
System.IO.StreamReader reader = new System.IO.StreamReader(fs);
if (alist== null)
{
alist= new ArrayList();
}
else
{
alist.Clear();
}
alist.Add(reader);

reader.Close();
reader.Dispose();

fs.Close();
fs.Dispose();
}

}

}

Here is a similar example:

private void StopWatcher()
    {
      if (_fsWatcher != null)
      {
        try
        {
          _fsWatcher.EnableRaisingEvents = false;
          _fsWatcher.Dispose();
          _fsWatcher = null;
        }
        catch { }
      }
    }
    /* -------------------------------------------------------------------- */
    private void StartWatcher(string ExeConfig)
    {
      StopWatcher();

      FileSystemWatcher _fsWatcher = new FileSystemWatcher();
      _fsWatcher.Path = Path.GetDirectoryName(ExeConfig);
      _fsWatcher.Filter = Path.GetFileName(ExeConfig);
      _fsWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
      _fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Created += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Deleted += new FileSystemEventHandler(OnChanged);
      _fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);
      _fsWatcher.EnableRaisingEvents = true;
    }
    /* -------------------------------------------------------------------- */
    private void OnChanged(object source, FileSystemEventArgs e)
    {
      ReloadConfig();
    }
    /* -------------------------------------------------------------------- */
    private void OnRenamed(object source, RenamedEventArgs e)
    {
      ReloadConfig();
    }

Please take a look at my post
'Duplicate events with FIleSystemWatcher'

Half of the code I have already :) I have miles and miles of code for applications i've done over the years.

hi! Please take a look at my post 'Duplicate events with FIleSystemWatcher'

Hi,
please help me with this,
'Duplicate events with FIleSystemWatcher '

Half of the code I have already :) I have miles and miles of code for applications i've done over the years.

Let me know when you publish your stuff man... I want an autographed copy too it that can be arranged! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.