public bool TryRename(string newName)
{
if (newName == dir.Name)
{
return true;
}
string newPath = Path.Combine(ParentPath, newName);
bool caseChange = newName.Equals(dir.Name, StringComparison.OrdinalIgnoreCase);
if (Directory.Exists(newPath) && !caseChange)
{
return false;
}
DisableAllWatchers();
DirectoryInfo oldPath = dir;
dir = new DirectoryInfo(newPath);
if (!caseChange)
{
this.UpdateChildren();
}
try
{
if (caseChange)
{
string crazyPath = Path.Combine(ParentPath, Path.GetRandomFileName());
oldPath.MoveTo(crazyPath);
Directory.Move(crazyPath, newPath);
}
else
{
oldPath.MoveTo(newPath);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.StackTrace);
dir = oldPath;
if (!caseChange)
{
this.UpdateChildren();
}
EnableAllWatchers();
return false;
}
EnableAllWatchers();
parent.RefreshIcons();
return true;
}