DirectoryInfo.Rename(To)またはFileInfo.Rename(To)メソッドがどこにも見つかりませんでした。だから、私は自分で書いたので、必要に応じて誰でも使用できるようにここに投稿します。それに直面しましょう。MoveToメソッドはやり過ぎであり、ディレクトリまたはファイルの名前を変更するだけの場合は、常に追加のロジックが必要になります。
public static class DirectoryExtensions
{
public static void RenameTo(this DirectoryInfo di, string name)
{
if (di == null)
{
throw new ArgumentNullException("di", "Directory info to rename cannot be null");
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("New name cannot be null or blank", "name");
}
di.MoveTo(Path.Combine(di.Parent.FullName, name));
return; //done
}
}