public void Undo() { // 現在の背景のパスを取得する。 var currentPath = DesktopBackgroundImage.GetCurrentPath(); if (string.IsNullOrWhiteSpace(currentPath)) { 最新の画像を設定(); return; } string currentFullPath; try { currentFullPath = Path.GetFullPath(currentPath); } catch { // GetFullPath で例外が発生するということは、レジストリに無効なパスが設定されていたということ。 // この場合は、このプログラムが分かる最も新しい画像を設定する。 最新の画像を設定(); return; } var directoryName = Path.GetDirectoryName(currentFullPath); if (directoryName == currentDirectory) { // 現在の背景のパスがカレントディレクトリの画像を指しているならば、一つ古い画像を設定する。 最新のバックアップを設定(); return; } if (directoryName == backupDirectory) { // 現在の背景のパスが Backup 内の画像を指しているならば、その画像より一つ古い画像を設定する。 // そのような画像が無ければ何もしない。 if (!Directory.Exists(backupDirectory)) { return; } var currentFileName = Path.GetFileName(currentPath); var files = Directory.EnumerateFiles(backupDirectory) .Select(f => new { Name = Path.GetFileName(f), Path = f }) .Where(f => f.Name.CompareTo(currentFileName) < 0) .OrderByDescending(f => f.Name); if (files.Any()) { SetBackgroundImage(Path.GetFullPath(files.First().Path)); } return; } // ここに到達するということは、有効ではあるが全くあずかり知らぬパスが設定されているということ。 最新の画像を設定(); }
public void Redo() { // 現在の背景のパスを取得する。 var currentPath = DesktopBackgroundImage.GetCurrentPath(); // 現在の背景のパスが無効なパスならばやり直せない。 string currentFullPath; try { currentFullPath = Path.GetFullPath(currentPath); } catch { // GetFullPath で例外が発生するということは、レジストリに無効なパスが設定されていたということ。 // この場合は、やり直せない。 return; } // 現在の背景のパスが Backup 内の画像を指していなければ、やり直せない。 var directoryName = Path.GetDirectoryName(currentPath); if (directoryName != backupDirectory) { return; } // 現在の背景のパスが Backup 内の画像を指しているならば、その画像より一つ新しい画像を設定する。 if (!Directory.Exists(backupDirectory)) { カレントディレクトリの画像を設定(); return; } var currentFileName = Path.GetFileName(currentPath); var files = Directory.EnumerateFiles(backupDirectory) .Select(f => new { Name = Path.GetFileName(f), Path = f }) .Where(f => f.Name.CompareTo(currentFileName) > 0) .OrderBy(f => f.Name); if (files.Any()) { SetBackgroundImage(Path.GetFullPath(files.First().Path)); } else { カレントディレクトリの画像を設定(); } }