scott scott - 1 year ago 76 Question

Renaming Directory with same name different case

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");

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?

Answer Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download