文件或文件夹重命名为小写在 C# 中使用 DirectoryInfo/FileInfo.MoveTo()

发布于 2024-12-15 18:20:53 字数 1571 浏览 1 评论 0原文

我有一个程序可以将文件或文件夹重命名为小写名称。我写了这段代码:

    private void Replace(string FolderLocation, string lastText, string NewText)
    {
        if (lastText == "")
        {
            lastText = " ";
        }
        if (NewText == "")
        {
            NewText = " ";
        }

        DirectoryInfo i = new DirectoryInfo(FolderLocation);
        string NewName = "";
        if (checkBox2.Checked)
        {
            if (i.Parent.FullName[i.Parent.FullName.Length - 1].ToString() != "\\") //For parents like E:/
            {
                NewName = i.Parent.FullName + "\\" + i.Name.Replace(lastText, NewText);
            }
            else
            {
                NewName = i.Parent.FullName + i.Name.Replace(lastText, NewText);
            }

                NewName = NewName.ToLower();


            if (NewName != i.FullName)
            {
                 i.MoveTo(NewName);
            }
            foreach (DirectoryInfo sd in i.GetDirectories())
            {
                Replace(sd.FullName, lastText, NewText);
            }
        }
        if (checkBox1.Checked)
        {
            foreach (FileInfo fi in i.GetFiles())
            {
                NewName = fi.Directory + "\\" + fi.Name.Replace(lastText, NewText);

                    NewName = NewName.ToLower();

                if (NewName != fi.FullName)
                {
                    fi.MoveTo(NewName);
                }
            }
        }
    }

但我得到以下异常:

“源路径和目标路径必须不同。”

我该如何解决这个问题?

I have a program that renames files or folders to lower case names. I have written this code:

    private void Replace(string FolderLocation, string lastText, string NewText)
    {
        if (lastText == "")
        {
            lastText = " ";
        }
        if (NewText == "")
        {
            NewText = " ";
        }

        DirectoryInfo i = new DirectoryInfo(FolderLocation);
        string NewName = "";
        if (checkBox2.Checked)
        {
            if (i.Parent.FullName[i.Parent.FullName.Length - 1].ToString() != "\\") //For parents like E:/
            {
                NewName = i.Parent.FullName + "\\" + i.Name.Replace(lastText, NewText);
            }
            else
            {
                NewName = i.Parent.FullName + i.Name.Replace(lastText, NewText);
            }

                NewName = NewName.ToLower();


            if (NewName != i.FullName)
            {
                 i.MoveTo(NewName);
            }
            foreach (DirectoryInfo sd in i.GetDirectories())
            {
                Replace(sd.FullName, lastText, NewText);
            }
        }
        if (checkBox1.Checked)
        {
            foreach (FileInfo fi in i.GetFiles())
            {
                NewName = fi.Directory + "\\" + fi.Name.Replace(lastText, NewText);

                    NewName = NewName.ToLower();

                if (NewName != fi.FullName)
                {
                    fi.MoveTo(NewName);
                }
            }
        }
    }

But I get the following exception:

"Source and destination path must be different."

How can I solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

ま昔日黯然 2024-12-22 18:20:54

由于 Windows 不区分大小写,因此就文件名而言,您需要将文件重命名为临时名称,然后用小写字符重命名回来。

Since Windows is case insensitive, as far as file names are concerned, you will need to rename the file to a temporary name then rename back with lowercase characters.

无所谓啦 2024-12-22 18:20:54

尽管 Windows 文件系统存储名称区分大小写,但它们在名称比较时不区分大小写,因此您的重命名操作将不起作用...

如果您确实需要/想要这样做,则需要首先将文件/目录临时重命名为不同的名称并且是唯一的,然后将其重命名为您想要的“小写名称”。

有关参考,请参阅 http://msdn.microsoft。 com/en-us/library/ee681827%28v=vs.85%29.aspxhttp://support.microsoft.com/kb/100108/en-us

如果您需要 NTFS 区分大小写,可以将 HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ 下的双字 ObCaseInsensitive 设置为 0(不推荐!) 。

如果您正在处理 NFS,请参阅 http://technet .microsoft.com/en-us/library/cc783185%28WS.10%29.aspx

Although Windows Filesystems store names case-senstivie they behave case-insensitive on name comparison thus your renaming operation won't work...

IF you really need/want to do that you will need to first rename temporarily the file/directory to something different and unique, then rename it "back" to the "lower case name" you want.

For reference see http://msdn.microsoft.com/en-us/library/ee681827%28v=vs.85%29.aspx and http://support.microsoft.com/kb/100108/en-us .

IF you need NTFS to be case-sensitive you can set the dword ObCaseInsensitive under HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ to 0 (NOT RECOMMENDED!).

IF you are dealing with NFS then see http://technet.microsoft.com/en-us/library/cc783185%28WS.10%29.aspx .

君勿笑 2024-12-22 18:20:54

这有效:

File.Move(destinationFilePath, destinationFilePath);

This works:

File.Move(destinationFilePath, destinationFilePath);
冬天旳寂寞 2024-12-22 18:20:54

不幸的是,这是一个 Windows 问题,因为正如 Oded 在评论中提到的那样,它不区分大小写。您所要做的就是将该文件夹重命名两次。通过将文件夹移动到新的临时名称,然后返回到原始名称的小写字母。

Unfortunately this is a windows issue as it is case insensitive as Oded mentions in the comments. What you would have to do is to rename the folder twice. By moving the folder to a new temporary name then back to the lowercase of the original name.

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