private static void SetWallpaperInternal(String path, WallpaperArrangement?arrangement, Boolean resetable) { if (path == null) { throw new ArgumentNullException("path"); } WallpaperArrangement?oldArrangement = null; String oldPath = null; // If the path is empty we just want to set the wallpaper to nothing if (path.Length != 0) { new FileIOPermission(FileIOPermissionAccess.Read, path).Demand(); } if (resetable) { try { // This will require read rights too, but its necessary to execute this method safety oldPath = Desktop.GetWallpaperPath(); } catch { oldPath = null; } } // Do we want to set a new arrangement? if (arrangement != null) { if (resetable) { // This will require read rights too, but its necessary to execute this method safety oldArrangement = Desktop.GetWallpaperArrangement(); } Desktop.SetWallpaperArrangement(arrangement.Value); } Int32 errorOccurred = WinAPI.SystemParametersInfo( WinAPI.SPI_SETDESKWALLPAPER, 1, path, WinAPI.SPIF_UPDATEINIFILE | WinAPI.SPIF_SENDWININICHANGE ); // Check if an error occurred if (errorOccurred == 0) { if (resetable) { // Since we had an error when setting the wallpaper, we have to try to reset the registry values now if ((arrangement != null) && (oldArrangement != null)) { Desktop.SetWallpaperArrangement(oldArrangement.Value); } if (oldPath != null) { Desktop.SetWallpaperInternal(oldPath, null, false); } } throw new Win32Exception(Marshal.GetLastWin32Error()); } }
/// <inheritdoc cref="SetWallpaper(Path, Nullable{WallpaperArrangement})" /> public static void SetWallpaper(Path filePath) { Desktop.SetWallpaper(filePath, null); }
/// <summary> /// Sets no wallpaper on the windows desktop. /// </summary> /// <inheritdoc /// cref="SetWallpaper(Path, Nullable{WallpaperArrangement})" /// select='*[@cref!="FileIOPermission"]' /// /> public static void SetWallpaperToNone() { Desktop.SetWallpaperInternal("", null, true); }
/// <summary> /// Sets a new wallpaper to be shown on the Windows desktop. /// </summary> /// <remarks> /// This method was tested on Windows Vista Business and worked fine with Bitmap and Jpeg files. /// However, the API documentation says that the API function used by this method only supports /// Bitmap files so be carefully when using the method on other operating systems. /// </remarks> /// <param name="filePath">The path of the image file.</param> /// <param name="arrangement"> /// The new arrangement of the wallpaper. Set to <c>null</c> to keep the arrangement unchanged. /// </param> /// <exception cref="SecurityException"> /// Missing framework access rights to read the file specified by <see cref="filePath"/>. /// Or missing framework access rights to read or write the registry key or its values. /// Or missing Windows access rights to read or write the registry key. /// </exception> /// <exception cref="ArgumentNullException"> /// <see cref="filePath"/> is set to <c>null</c>. /// </exception> /// <exception cref="UnauthorizedAccessException"> /// Missing Windows access rights to read the registry values. /// </exception> /// <exception cref="Win32Exception"> /// The file specified by <see cref="filePath"/> doesnt exist. /// </exception> /// <inheritdoc cref="SetWallpaperPath" select='permission[@cref="RegistryPermission"]' /> /// <permission cref="FileIOPermission"> /// for reading the wallpaper file. Associated enumerations: /// <see cref="FileIOPermissionAccess.PathDiscovery">FileIOPermissionAccess.PathDiscovery</see>, /// <see cref="FileIOPermissionAccess.Read">FileIOPermissionAccess.Read</see>. /// </permission> public static void SetWallpaper(Path filePath, WallpaperArrangement?arrangement) { new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, filePath).Demand(); Desktop.SetWallpaperInternal(filePath, arrangement, true); }