Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

c# - 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:est 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:est");
dir.MoveTo(@"F:est2");
dir.MoveTo(@"F:TEST");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...