/// <summary> /// Perform backups of jpg and png files in the specified directory. Files with an existing backup are ignored. /// </summary> /// <param name="targetDir">Target directory</param> /// <param name="move">If enabled, remove the original files after performing a backup</param> /// <param name="overwriteWithImage">If specified, also replace all the original images with the specified image file</param> public static void PerformBackupReplace(string targetDir, bool removeOriginal, string overwriteWithImage = null) { foreach (string picture in Directory.GetFiles(targetDir, "*.jpg").Union(Directory.GetFiles(targetDir, "*.png"))) { string pictureBackup = picture + ".bak"; if (!File.Exists(pictureBackup)) { if (removeOriginal) { File.Move(picture, pictureBackup); } else { File.Copy(picture, pictureBackup); } } if (overwriteWithImage != null) { if (!File.Exists(overwriteWithImage)) { throw new ArgumentException("The specified image file does not exist.", "overwriteWithImage"); } ImageEncoder.AutoCopyImageFile(overwriteWithImage, picture); } } }
/// <summary> /// Replace the system lockscreen image for Windows 7, 8 and 10. /// </summary> /// <remarks>Administrator permissions are required</remarks> /// <param name="path">Path to the new .jpg file for global lockscreen</param> public static void SetGlobalLockscreen(string path) { if (WindowsVersion.IsMono) { throw new NotImplementedException("Lockscreen handling is not implemented for Mac and Linux using the Mono framework."); } else if (WindowsVersion.WinMajorVersion == 6 && WindowsVersion.WinMinorVersion == 1 /* Windows 7 */) { string lockscreenDir = Win7_InitDir(); string lockscreenPic = String.Concat(lockscreenDir, Path.DirectorySeparatorChar, "backgroundDefault.jpg"); PerformBackupReplace(lockscreenDir, true); ImageEncoder.CreateJpeg(path, lockscreenPic, 256000); Win7_RegistryKey(true); //Create a dummy backup if the machine contains no OEM background image if (!File.Exists(lockscreenPic + ".bak")) { File.Create(lockscreenPic + ".bak").Close(); } } else if ((WindowsVersion.WinMajorVersion == 6 && WindowsVersion.WinMajorVersion >= 2) || /* Windows 8 and 10 */ WindowsVersion.WinMajorVersion >= 10 /* Windows 10 and future Windows versions - might not work on newer versions */) { string lockscreenDir = Win10_InitDir(); PerformBackupReplace(lockscreenDir, false, path); Win10_ClearCache(); } else { throw new NotImplementedException( String.Format( "Lockscreen handling is not implemented for Windows NT {0}.{1}", WindowsVersion.WinMajorVersion, WindowsVersion.WinMinorVersion ) ); } }