Trying to Copy a changed file to backup folder
-
Hi All! I want to use this code to copy a changed file to backup folder, i am using Filesystemwatcher to listen for the change to a file in a test folder in documents, and when a file is changed a copy should end up in backup. Now when i use File.Copy it copies the file but then gives me an exception saying it allready exists! How can this be when the backup folder was empty?! i checked this out, and if i use File.Move it still complains when it moves the folder to backup, and the says "file does not exist in test!" so copy would be better as it leaves the original where it is, but why the hell does it say it allready exists in backup when it copies it over?? any ideas? im doin the 70-536 course and the "suggested practices" at the end of the chapters just chuck you in the deep end! lol. //set the FileSystemWatcher properties fsd.IncludeSubdirectories = true; fsd.Path = @"C:\Users\Luke\Documents\test"; fsd.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite; //add the changed event handler fsd.Changed += new FileSystemEventHandler(fsd_Changed); fsd.EnableRaisingEvents = true; static void fsd_Changed(object sender, FileSystemEventArgs e) { //allready exists on first copy!!?? File.Copy(@"C:\Users\Luke\Documents\test\" + e.Name, @"C:\Users\Luke\Documents\test\Backup\" + "bak" + e.Name); //moves file and then says "could not find file in test! File.Move(@"C:\Users\Luke\Documents\test\" + e.Name, @"C:\Users\Luke\Documents\test\Backup\" + "bak" + e.Name); }
-
Hi All! I want to use this code to copy a changed file to backup folder, i am using Filesystemwatcher to listen for the change to a file in a test folder in documents, and when a file is changed a copy should end up in backup. Now when i use File.Copy it copies the file but then gives me an exception saying it allready exists! How can this be when the backup folder was empty?! i checked this out, and if i use File.Move it still complains when it moves the folder to backup, and the says "file does not exist in test!" so copy would be better as it leaves the original where it is, but why the hell does it say it allready exists in backup when it copies it over?? any ideas? im doin the 70-536 course and the "suggested practices" at the end of the chapters just chuck you in the deep end! lol. //set the FileSystemWatcher properties fsd.IncludeSubdirectories = true; fsd.Path = @"C:\Users\Luke\Documents\test"; fsd.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite; //add the changed event handler fsd.Changed += new FileSystemEventHandler(fsd_Changed); fsd.EnableRaisingEvents = true; static void fsd_Changed(object sender, FileSystemEventArgs e) { //allready exists on first copy!!?? File.Copy(@"C:\Users\Luke\Documents\test\" + e.Name, @"C:\Users\Luke\Documents\test\Backup\" + "bak" + e.Name); //moves file and then says "could not find file in test! File.Move(@"C:\Users\Luke\Documents\test\" + e.Name, @"C:\Users\Luke\Documents\test\Backup\" + "bak" + e.Name); }
Why all the white space? Anyway... have you set a breakpoint at (or just after) the offending line? It is possible for FileSystemWatcher to fire the same event multiple events in a very short space of time. My guess is it may be suceeding the first time, but the event may be immediately being reraised.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)