Rename via Directory.Move gives Access Denied Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smjhunt
    New Member
    • Feb 2010
    • 2

    Rename via Directory.Move gives Access Denied Error

    I get this error intermittently in error reports submitted from users. Can't reproduce it myself. Most seem to be running XP so it's not a Vista issue.

    My C# code creates a temporary folder then copies files to it. At the end it removes the Temp from the name and replaces it with a date stamped name:

    backupPath = destFolder + "\\Backup_" + String.Format(" {0:yyyyMMdd_HHm mss}", DateTime.Now);

    Directory.Move( destFolder + "\\Temp_Backup" ,backupPath);

    The Move operation throws an exception: Access to the path xxx is denied.
    where xxx is the temp folder path (i.e. destFolder+"\\T emp_Backup);

    Note that this is the folder into which I had just successfully copied one or more files. Note also that I'm not moving the folder but just renaming it.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    If it is over a network at the client's yet local PC at your testing that might explain it. Sometimes you need to give time to servers to complete the task. They will lie to client PC's and tell you a copy is complete when it really isn't.

    I try to simulate poor client networks at home by using two slow WiFi connected computers as server and client.

    I would suggest that you keep trying to do the rename for 15 seconds, then bail out of the retry loop either when the time is up, or when you have done it successfully.

    I can make some general suggestions that apply to good coding practice.
    • Assume that everything is broken, or at least not ideal.
    • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
    • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
    • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
    • Check that files/folders exist, even if you just did a call to make it: You may not have permissions.
    • Don't assume the harddrive has room for what you are doing: They do fill up. Usually right in the middle of you writing to your log file.
    • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
      • Put a breakpoint on the first line of the method causing trouble.
      • When the code stops there, walk through line by line with F-10.
      • Check the values of your assumptions (looking at the Locals and Automatic variable windows as well as hovering the mouse over the variables in the code (hothelp will popup).
    • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
    • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.

    Comment

    • smjhunt
      New Member
      • Feb 2010
      • 2

      #3
      The folder itself is on the user's local drive. The files I'm copying into it do not come from a network drive but they come from a fairly slow USB flash drive. Perhaps this is part of the problem although, as I said, the folder I am trying to rename is local.

      Comment

      Working...