2

I'm using Directory.Move(oldDir, newDir) to rename a directory. Every now and then I get an 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.

So my question is: Are there any other ways to rename a directory without using Directory.Move?

I thought of using the command shell (Process.Start()) but this should be my last way of doing it.


Further details

The program is still running, I get the exception and can rename it manually in windows explorer (right click -> rename) while my cursor is paused on the breakpoint in the catch block. 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.

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.

Since this way is not working for me I need to find another way to do so.


Solution

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

    SHFILEOPSTRUCT struc = new SHFILEOPSTRUCT();

    struc.hNameMappings = IntPtr.Zero;
    struc.hwnd = IntPtr.Zero;
    struc.lpszProgressTitle = "Rename Release directory";
    struc.pFrom = destPathRelease + '\0';
    struc.pTo = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(this.currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : this.currBranchName) + '.' + this.currBuildLabel + '\0';
    struc.wFunc = FileFuncFlags.FO_RENAME;

    int ret = SHFileOperation(ref struc);
}

Please note it's important to use pTo and pFrom as zero delimited double zero terminated strings.

theknut
  • 2,533
  • 5
  • 26
  • 41
  • http://stackoverflow.com/a/2024022/961113 – Habib May 15 '13 at 09:03
  • This will probably be to do with a file in the folder being `Read-only` and will have certain rights restricted – LukeHennerley May 15 '13 at 09:04
  • @Habib not sure what you are trying to say since there are no other methods as `Directory.Move` in this thread. @LukeHennerley if they would be read-only why can I rename them in the windows explorer? @both Have you read the full question? (not trying to be mean in any way) – theknut May 15 '13 at 09:12
  • 2
    @theknut Are you running this code from Visual studio with admin rights? if not run as admin and check, and also try to give full permission to all users to your source folder and test. – Damith May 15 '13 at 09:26

2 Answers2

2

You could try using P/Invoke to call the SHFileOperation API (or the IFileOperation interface). This is what Explorer uses in theory, so it should replicate the functionality more directly.

Jon Grant
  • 11,369
  • 2
  • 37
  • 58
0

Try using DirectoryInfo.MoveTo() instead of Directory.Move().

Also, check this old SO question: it doesn't seems to have the same exception you're figuring out, but you can find some good advice.

Community
  • 1
  • 1
Spaceman
  • 547
  • 8
  • 25