开发者

Windows batch command to make changes effective immediately

开发者 https://www.devze.com 2023-04-02 14:28 出处:网络
I have a batch script that lets users change their background from black to white or vice versa.The problem I\'m having is that the script only makes immediate change sometimes, and other times the us

I have a batch script that lets users change their background from black to white or vice versa. The problem I'm having is that the script only makes immediate change sometimes, and other times the user has to log off and log back on for the background to change. Here is what I have so far:

@echo off

call :quiet
exit /b

:quiet
    :: For comparison, using the black wallpaper registry value
    set "black=C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp"

    :: Set reg query result to current
    FOR /F "tokens=2* delims=    " %%A IN ('REG QUERY "HKCU\Control Panel\Desktop" /v Wallpaper') DO SET current=%%B
    :: For debugging purpose.
    ECHO current=%current%
    pause

    if "%current%"=="%black%" (
        call :MakeDayWallpaper>nul 2>&1
        :: Make changes without requiring logoff
        RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
    ) else 开发者_C百科(
        call :MakeNightWallpaper>nul 2>&1
        :: Make changes without requiring logoff
        RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
    )
EXIT /b


:MakeDayWallpaper
    REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
    REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_white.bmp" /f
    REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
    REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
EXIT /b


:MakeNightWallpaper
    REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
    REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp" /f
    REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
    REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
EXIT /b

The line RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters is the command that allows the immediate change. When I look at shell I can see that the registry value is changing every time the script is executed, but despite this fact, sometimes the background does not change until the user logs off and logs on.


It might be that the registry changes aren't taking effect until the log off/log on is done (I'm not sure why it would sometimes work immediately though). Try restarting explorer afterwards and see if that helps.

taskkill /im explorer.exe /f
explorer.exe
0

精彩评论

暂无评论...
验证码 换一张
取 消