File or Folder rename to lower case In C# using DirectoryInfo/FileInfo.MoveTo()

允我心安 提交于 2019-12-25 05:36:09

问题


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 an exception:

"Source and destination path must be different."

how can I resolving this problem?


回答1:


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.




回答2:


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 .




回答3:


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.



来源:https://stackoverflow.com/questions/8152731/file-or-folder-rename-to-lower-case-in-c-sharp-using-directoryinfo-fileinfo-move

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!