我有一个重命名文件或文件夹小写名字的程序。
我写了这个代码:
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);
}
}
}
}
但我得到一个异常:
“源和目标路径必须是不同的。”
我怎样才能解决这个问题呢?