C# - Ways to rename a directory
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.
Comments
-
theknut almost 2 years
I'm using
Directory.Move(oldDir, newDir)
to rename a directory. Every now and then I get anIOException
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 overDirectory.Move
and ended up in thecatch (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
andpFrom
as zero delimited double zero terminated strings.-
Habib almost 11 years
-
LukeHennerley almost 11 yearsThis will probably be to do with a file in the folder being
Read-only
and will have certain rights restricted -
theknut almost 11 years@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) -
Damith almost 11 years@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.
-
-
theknut almost 11 yearsMy hero! How do you actually know all this? Just being curious...