Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to Copy a changed file to backup folder

Trying to Copy a changed file to backup folder

Scheduled Pinned Locked Moved C#
questionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luke Perrin
    wrote on last edited by
    #1

    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); }

    D 1 Reply Last reply
    0
    • L Luke Perrin

      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); }

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups