I'm hoping someone can help me. I'm using FileSystemWatcher 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\file.txt to c:\file.txt) I get an UnauthorizedAccessException error.
It looks like when a file is created or changed in the sub folder the FileSystemWatcher 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:
Expand|Select|Wrap|Line Numbers
- File.Copy(e.FullPath + @"\file.txt", NetworkArchive + @"\file.txt", true);
Expand|Select|Wrap|Line Numbers
- 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);
- }