views:

251

answers:

3

I am trying to rename a directory in c# to a name that is the same only with differing case.

For example: f:\test to f:\TEST

I have tried this code:

var directory = new DirectoryInfo("f:\\test");
directory.MoveTo("f:\\TEST");

and I get a IOException - Source and destination path must be different. I have also tried Directory.Move() with the same result.

How is this done? Do I have to create a separate temp directory, move the contained files from the original directory to the temp directory, and then delete the original, and rename the temp directory?

A: 

The answer is yes in this case - the file system itself doesn't see the two as different, so you'll need to delete and the add as the new name (or move/delete/move as you suggested)

cynicalman
The file system itself does. The Windows API wrapper around it introduces case-insensitivity.
Joey
+1  A: 

Why not rename the directory temp and then rename again to TEST ?

pavium
+4  A: 

Well, you don't need to create a separate directory and move everything. Just rename the folder to something different and then back to the name you want:

var dir = new DirectoryInfo(@"F:\test");
dir.MoveTo(@"F:\test2");
dir.MoveTo(@"F:\TEST");

Apparently (big question mark, though), you could use MoveFileEx with the MOVEFILE_REPLACE_EXISTING flag.

Joey
thanks, that makes a lot more sense than the way I described doing it.
scottman666