重命名具有相同名称不同大小写的目录
我试图将 C# 中的目录重命名为仅在不同情况下才相同的名称。
例如: f:\test 到 f:\TEST
我尝试过这段代码:
var directory = new DirectoryInfo("f:\\test");
directory.MoveTo("f:\\TEST");
并且得到 IOException - 源路径和目标路径必须不同。我也尝试过 Directory.Move() ,结果相同。
这是怎么做到的?我是否必须创建一个单独的临时目录,将包含的文件从原始目录移动到临时目录,然后删除原始目录,并重命名临时目录?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么,您不需要创建单独的目录并移动所有内容。只需将文件夹重命名为不同的名称,然后返回到您想要的名称:
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:
即使 .NET 方法
DirectoryInfo.MoveTo
在名称相同的情况下引发异常,您也可以调用 Windows APIMoveFile
像这样的函数来设置目录名称的大小写:使用这个外部声明:
它对我来说效果很好名称仅大小写不同。当名称已经完全符合指定时,我还没有尝试过这样调用它。
这样做的优点是目录永远不会以其原始名称消失。
但它有一个缺点,即它只能在 Windows 上运行。
Even if the .NET method
DirectoryInfo.MoveTo
throws an exception if the name is the same, you can call the Windows APIMoveFile
function like this to set the casing of the directory name:With this extern declaration:
It works fine for me when the name differs in case only. I haven't tried calling it like this when the name is already exactly as specified.
This has the advantage that the directory never disappears under its original name.
It has the disadvantage though that it only works on Windows.
为什么不重命名目录
temp
然后再次重命名为TEST
?Why not rename the directory
temp
and then rename again toTEST
?在这种情况下答案是肯定的 - 文件系统本身并不认为两者不同,因此您需要删除并添加为新名称(或按照您的建议移动/删除/移动)
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)