我有一個程序,將文件或文件夾重命名爲小寫名稱。文件或文件夾重命名爲小寫在C#中使用DirectoryInfo/FileInfo.MoveTo()
我寫了這個代碼:
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);
}
}
}
}
,但我得到一個異常: 「源和目標路徑必須是不同的」
我該如何解決這個問題?
就文件名而言,Windows不區分大小寫。 – Oded
ok.but它如何顯示文件名? –
它使用保存的內容。但是,在比較文件名時 - 「FILENAME」==「文件名」==「文件名」',就其而言。 – Oded