FileSystemWatcher IncludeSubdirectories problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul73
    New Member
    • Sep 2007
    • 13

    FileSystemWatcher IncludeSubdirectories problem

    Hi everyone,

    I'm hoping someone can help me. I'm using FileSystemWatch er to watch a folder on the network that'll copy files to a local drive. Everything works fine when files are added to the root directory. So x:\file.txt is copied to c:\file.txt.

    But when I drop a file into a subdirectory (ie. x:\subFolder\fi le.txt to c:\file.txt) I get an UnauthorizedAcc essException error.

    It looks like when a file is created or changed in the sub folder the FileSystemWatch er isn't returning the file name. So it's trying to copy x:\ to c:\.

    If I hardcode the file name like this it works:
    Code:
     
    File.Copy(e.FullPath + @"\file.txt", NetworkArchive + @"\file.txt", true);
    Here is the code I'm using:

    Code:
     
    private void Form1_Load(object sender, EventArgs e)
    {
    
    FileSystemWatcher watch = new FileSystemWatcher();
    
    watch.Path = NetworkDrive;
    watch.IncludeSubdirectories = true;
    watch.Filter = "*.*";
    watch.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
    watch.EnableRaisingEvents = true;
    
    
    //EVENT HANDLER
    watch.Created += new FileSystemEventHandler(watch_Event);
    watch.Changed += new FileSystemEventHandler(watch_Event);
    watch.Renamed += new RenamedEventHandler(watch_Event);
    
    }
    
    
    static void watch_Event(object sender,FileSystemEventArgs e)
    {
    string LocalDrive = Settings1.Default.LocalDrive;
    string NetworkArchive = Settings1.Default.NetworkArchive;
    
    //COPY FILE TO LOCAL DRIVE
    File.Copy(e.FullPath, NetworkArchive + e.Name, true);
    
    }
    Thank you for any help!
Working...