2

I'm using Directory.Move(oldDir, newDir) to rename a directory. Every now and then I get a IOException saying "Access to the path "oldDir" is denied". However if I right click the directory in the explorer I can rename it without any issues. How's that and how can I get it to work?

EDIT

The program is still running, I get the exception and can rename it manually while my cursor is paused on the breakpoint. I also tried setting a breakpoint at Directory.Move, successfully renamed the directory in explorer (and back again), stepped over Directory.Move and ended up in the catch (IOException) again. So I don't see why my program should lock the directory at all. There must be something else.

Any ideas?

EDIT 2

Here is my code

public bool Copy()
{
    string destPathRelease = ThisUser.DestPath + "\\Release";

    if (Directory.Exists(destPathRelease))
    {
        try
        {
            string newPath = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : currBranchName) + '.' + currBuildLabel;
            Directory.Move(destPathRelease, newPath);

            catch (IOException)
            {
               // Breakpoint
            }
        }
    }
}

As you can see I just entered the method. I never touched the directory in my program before. Is there another way to rename a directory?

theknut
  • 2,533
  • 5
  • 26
  • 41

4 Answers4

3

Without seeing more code I'd say your application is locking a file within the directory, you can see what is accessing the directory using Process explorer

from the intro to process explorer:

Ever wondered which program has a particular file or directory open? Now you can find out. Process Explorer shows you information about which handles and DLLs processes have opened or loaded.

It might also be worth making sure nothing else is copying files from/to that directory - e.g. dropbox. I had an issue recently where visual studio would stop debugging because of a file lock - in the end it was indexing on the drive which was temporarily locking the file. Process explorer only partially helped in that it showed 'system' had the file lock and not another application.

NDJ
  • 5,189
  • 1
  • 18
  • 27
  • Completely forgot about Process explorer. I will test it right now. Please also see my comments in Stefano Altieri's answer. – theknut Apr 29 '13 at 13:58
  • Couldn't find anything useful, sadly. As I mentioned in Stefano Altieri's answer I'm on a breakpoint in the IOException and can manually rename the directory. That's odd as ... y'know? – theknut Apr 29 '13 at 14:23
  • could it be that when you get your exception, whatever is holiding the lock releases it? (e.g. you break out from a using block, etc.)? – NDJ Apr 29 '13 at 14:29
  • Just having the problem again. I've set a breakpoint at `Directory.Move`, successfully renamed the directory in explorer (and back again), stepped over `Directory.Move`and ended up in the `catch (IOException)` again. Any ideas? :'( – theknut May 14 '13 at 13:40
  • not so much, I think you'd be better starting a new question and showing the whole routine - e.g. what is executed when you step over Directory.Move that takes you to a catch – NDJ May 14 '13 at 15:15
  • Okay, I tried a different approach http://stackoverflow.com/questions/16560856/c-sharp-ways-to-rename-a-directory – theknut May 15 '13 at 09:03
  • 1
    glad you got a solution! I learned something new from the answer too :) – NDJ May 15 '13 at 11:31
0

You need to check the User that is running the .net application. It don't have the right permission to execute the rename.

This is:

  • the user running the application pool for web applications
  • the logged application for console/winforms application
  • the configured user for services or scheduled tasks
Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
  • 1
    "Every now and then" means that in most cases it will work. Furthermore the user who renames the directory is the one who created it. Shouldn't be an issue. – theknut Apr 29 '13 at 13:49
  • Are you or other application using files inside the directory? – Stefano Altieri Apr 29 '13 at 13:51
  • I guess not since I can rename it using Windows explorer. I already checked that. – theknut Apr 29 '13 at 13:52
  • 1
    This is not enough. If you have an unused file lock within the code it will be released when the process ends or when the GC runs. Try to check FileStreams and ensure you are using "using blocks". @NDJ answer is also a good way to debug the problem. – Stefano Altieri Apr 29 '13 at 13:54
  • I can rename the directory while I'm on a breakpoint in the catch part. So the program is still running, I get the exception and can rename it manually. – theknut Apr 29 '13 at 13:57
0

If the parent directory of your destination directory does not exist, The Directory.Move operation with fail. I've just been trying to figure out something loosely similar to this.

Ibz
  • 518
  • 1
  • 8
  • 26
0

This is the safest method to rename a directory in the C# .NET Core with cross-platform.

    /// <summary>
    /// Renames a folder name
    /// </summary>
    /// <param name="directory">The full directory of the folder</param>
    /// <param name="newFolderName">New name of the folder</param>
    /// <returns>Returns true if rename is successfull</returns>
    public static bool RenameFolder(string directory, string newFolderName)
    {
        try
        {
            if (string.IsNullOrWhiteSpace(directory) ||
                string.IsNullOrWhiteSpace(newFolderName))
            {
                return false;
            }


            var oldDirectory = new DirectoryInfo(directory);

            if (!oldDirectory.Exists)
            {
                return false;
            }

            if (string.Equals(oldDirectory.Name, newFolderName, StringComparison.OrdinalIgnoreCase))
            {
                //new folder name is the same with the old one.
                return false;
            }

            string newDirectory;

            if (oldDirectory.Parent == null)
            {
                //root directory
                newDirectory = Path.Combine(directory, newFolderName);
            }
            else
            {
                newDirectory = Path.Combine(oldDirectory.Parent.FullName, newFolderName);
            }

            if (Directory.Exists(newDirectory))
            {
                //target directory already exists
                return false;
            }

            oldDirectory.MoveTo(newDirectory);

            return true;
        }
        catch
        {
            //ignored
            return false;
        }
    }
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55