static void Main(string[] args) { try { string wallpaperDirectory = GetWallpaperDirectory(); if (string.IsNullOrWhiteSpace(wallpaperDirectory)) { throw new Exception("壁紙格納フォルダの取得に失敗しました。"); } string[] fileNames = Directory.GetFiles(wallpaperDirectory); string[] imageFileNames = ExcludeFileUnusableInWallpaper(fileNames); if (imageFileNames.Length == 0) { throw new Exception("壁紙に使えるファイルが見つかりませんでした。"); } //現在設定されている壁紙を取得する。 DesktopWallpaper wallpaper = new DesktopWallpaper(); uint monitorCount = wallpaper.GetMonitorDevicePathCount(); string lastMonitorId = wallpaper.GetMonitorDevicePathAt(monitorCount - 1); string currentWallpaperFilename = wallpaper.GetWallpaper(lastMonitorId); //現在の壁紙が何番目か調べる。 int currentWallpaperNum = Array.IndexOf(imageFileNames, currentWallpaperFilename); //壁紙を変更する int wallpaperFileIndex = currentWallpaperNum; for (int i = 0; i < monitorCount; i++) { wallpaperFileIndex = GetNextIndex(wallpaperFileIndex, imageFileNames.Length); string monitorId = wallpaper.GetMonitorDevicePathAt((uint)i); wallpaper.SetWallpaper(monitorId, imageFileNames[wallpaperFileIndex]); } //正常終了したらログファイルを削除する。 File.Delete(LogFilePath); } catch (Exception ex) { //ログ出力 List <string> logMessage = new List <string>(); logMessage.Add(DateTime.Now.ToLongDateString()); logMessage.Add(DateTime.Now.ToLongTimeString()); logMessage.Add(ex.ToString()); File.WriteAllLines(LogFilePath, logMessage.ToArray()); //設定ファイル削除 File.Delete(SettingFilePath); } }