Windows10/11修改了壁纸不立即生效

文章目录
  • 概述
  • 解决办法
  • 概述

    在域环境下的计算机正常情况下修改了Windows系统的壁纸是需要重启才能生效的,因为在某些情况下,系统可能会缓存壁纸设置,导致更改无法立即生效。

    壁纸的缓存目录位于

    C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Themes\CachedFiles

    解决办法

    更改当前桌面壁纸让执行立即生效,Powershell 可以调用Windows API 实现,下面这个函数能立即更改当前壁纸。

    function Set-Wallpaper
    {
        param(
            [Parameter(Mandatory=$true)]
            $Path,
    
            [ValidateSet('Center', 'Stretch')]
            $Style = 'Stretch'
        )
    
        Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    namespace Wallpaper
    {
    public enum Style : int
    {
    Center, Stretch
    }
    public class Setter {
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
    public static void SetWallpaper ( string path, Wallpaper.Style style ) {
    SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
    RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
    switch( style )
    {
    case Style.Stretch :
    key.SetValue(@"WallpaperStyle", "2") ; 
    key.SetValue(@"TileWallpaper", "0") ;
    break;
    case Style.Center :
    key.SetValue(@"WallpaperStyle", "1") ; 
    key.SetValue(@"TileWallpaper", "0") ; 
    break;
    }
    key.Close();
    }
    }
    }
    "@
    
        [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
    }
    
    Set-Wallpaper -Path 'C:\background2.jpg'

    执行后,壁纸的缓存同时也立即刷新,效果如下所示。

    3

    1. 似水流年

      没有用过这种,是存成cmd格式然后执行吗?

      1. 诺诺

        保存为.ps1,然后用Powershell执行,其中代码末尾为壁纸的路径。

    2. 沉沦

      要我我就重启😂

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注

    SQL Server 日志回收记录
    SQL Server 日志回收记录
    备份Windows桌面图标个性化排序
    备份Windows桌面图标个性化排序
    Windows 新建AD域控服务器、FSMO管理
    Windows 新建AD域控服务器、FSMO管理
    微软 49 张图
    批量启用Google Chrome允许第三方Cookie
    批量启用Google Chrome允许第三方Cookie
    使用PowerShell后台静默安装Windows补丁
    使用PowerShell后台静默安装Windows补丁
    限制Windows本地用户允许登录时间
    限制Windows本地用户允许登录时间
    © 2025 诺诺博客如有侵权请联系删除 | 网站地图 | 百度统计 | 又拍云CDN加速
    为了获得更好的浏览效果 建议您使用IE8.0及以上版本浏览器登陆本站点 · 服务器托管于腾讯云
    📢 小站正在装修中,如页面异常请包涵!