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); }
L
Luke Perrin
@Luke Perrin
Posts
-
Trying to Copy a changed file to backup folder -
How to create an application that lists folders containing the most disk space?Hi All Does anyone have an answer to this? im using C# Visual studio 2008? So far I was thinking just loop through the first root folder, compare the length of the first file with the rest, and if the first files length is smaller then swap the bigger one with it, so at the end all the biggest files will be at the top.. DirectoryInfo dir = new DirectoryInfo(@"C:\"); foreach (FileInfo file in dir.GetFiles()) { Console.WriteLine(file.Name); foreach (FileInfo file2 in dir.GetFiles()) { if (file.Length < file2.Length) file2.MoveTo(file.ToString()); } } }