473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileSystemWatcher IncludeSubdirectories problem

13
Hi everyone,

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
  1.  
  2. File.Copy(e.FullPath + @"\file.txt", NetworkArchive + @"\file.txt", true);
  3.  
Here is the code I'm using:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4.  
  5. FileSystemWatcher watch = new FileSystemWatcher();
  6.  
  7. watch.Path = NetworkDrive;
  8. watch.IncludeSubdirectories = true;
  9. watch.Filter = "*.*";
  10. watch.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
  11. watch.EnableRaisingEvents = true;
  12.  
  13.  
  14. //EVENT HANDLER
  15. watch.Created += new FileSystemEventHandler(watch_Event);
  16. watch.Changed += new FileSystemEventHandler(watch_Event);
  17. watch.Renamed += new RenamedEventHandler(watch_Event);
  18.  
  19. }
  20.  
  21.  
  22. static void watch_Event(object sender,FileSystemEventArgs e)
  23. {
  24. string LocalDrive = Settings1.Default.LocalDrive;
  25. string NetworkArchive = Settings1.Default.NetworkArchive;
  26.  
  27. //COPY FILE TO LOCAL DRIVE
  28. File.Copy(e.FullPath, NetworkArchive + e.Name, true);
  29.  
  30. }
  31.  
  32.  
  33.  
Thank you for any help!
Jun 15 '11 #1
0 1451

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: cxw0106 | last post by:
Hello, I have some weird problem with the FileSystemWatcher. I have developed an application that monitors a network directory for file changes of a certain file type. The program runs well for...
0
by: Long Tran | last post by:
Hello The DirectoryMonitor sample codes demonstrate the .NET Framework System.IO FileSystemWatcher object works nicely but display only short file name (for example test.bdrg become test~1.bdr)...
3
by: zfeld | last post by:
My app is creating a word document which my app calls saveas() and saves it to a given directory that is being watched by a fileSystemWatcher. The problem is that the fileSystemEventHandler for...
1
by: Joshua Mann | last post by:
I have created a windows service which runs the FileSystemWatcher component. If the service is running and monitoring a directory (ex. "c:\temp") during heavy folder access the service generates...
5
by: zfeld | last post by:
I am serializing an object to XML and writing it to disk. whenever a change to my object occurs I call the save function to re-write to disk (see code below). I am monitoring the directory where...
1
by: Phil396 | last post by:
I have a windows service that uses a filesystemwatcher to wait for files and process them to a database. Sometimes a large group of files will be cut and paste for the filesystemwatcher to...
0
by: Vitaly S via DotNetMonster.com | last post by:
Hi. I have problem using FileSystemWatcher object when monitoring multiple directories on remote machine.(When i am doing same thing on local machine no problem). When I am trying to monitor...
0
by: HawkeEYE | last post by:
I have a service that implements a FileSystemWatcher to watch a folder on a networked machine. I have no problem mapping to the folder. My problem arises when I try to start the service. I get the...
1
by: yogesh | last post by:
hello I getting problem in FileSystemWatcher , I had wrriten the code for the FileSystemWatcher , when i dropping the new file in the directory , the WatcherEdi_Created event get fired , but...
3
by: =?Utf-8?B?UnVkeQ==?= | last post by:
Hello All! I may have posted this problem before, but it was awhile ago. I'm working in VS 2003. I have 10 file watch process going. It seems that I can only about 6or 7 to run at the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.