重命名具有相同名称不同大小写的目录

发布于 2024-08-08 10:41:07 字数 329 浏览 2 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

划一舟意中人 2024-08-15 10:41:07

那么,您不需要创建单独的目录并移动所有内容。只需将文件夹重命名为不同的名称,然后返回到您想要的名称:

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

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");
挽清梦 2024-08-15 10:41:07

即使 .NET 方法 DirectoryInfo.MoveTo 在名称相同的情况下引发异常,您也可以调用 Windows API MoveFile 像这样的函数来设置目录名称的大小写:

bool success = MoveFile(dirInfo.FullName, dirInfo.FullName);

使用这个外部声明:

[DllImport("kernel32", SetLastError = true)]
private static extern bool MoveFile(string lpExistingFileName, string lpNewFileName);

它对我来说效果很好名称仅大小写不同。当名称已经完全符合指定时,我还没有尝试过这样调用它。

这样做的优点是目录永远不会以其原始名称消失。

但它有一个缺点,即它只能在 Windows 上运行。

Even if the .NET method DirectoryInfo.MoveTo throws an exception if the name is the same, you can call the Windows API MoveFile function like this to set the casing of the directory name:

bool success = MoveFile(dirInfo.FullName, dirInfo.FullName);

With this extern declaration:

[DllImport("kernel32", SetLastError = true)]
private static extern bool MoveFile(string lpExistingFileName, string lpNewFileName);

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.

煮茶煮酒煮时光 2024-08-15 10:41:07

为什么不重命名目录 temp 然后再次重命名为 TEST

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

心安伴我暖 2024-08-15 10:41:07

在这种情况下答案是肯定的 - 文件系统本身并不认为两者不同,因此您需要删除并添加为新名称(或按照您的建议移动/删除/移动)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文