更改壁纸powershell

时间:2017-04-03 14:50:33

标签: powershell

您好我正在尝试制作一个小脚本,以便在每个给定时间更改我的壁纸 我有一个文件夹,其中的图片名称为1.bmp,2.bmp等

我制作了这个剧本,但它根本不起作用

PS D:\Téléchargements\images\Wallpapers> for($i=1; $i -le 6; $i++){
>> reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ 
/d D:\Téléchargements\images\Wallpapers\$i.bmp /f
>> Start-Sleep -s 10
>> rundll32.exe user32.dll, UpdatePerUserSystemParameters
>> Start-Sleep -s 2
>> }

有人可以解释为什么请:(

PS:开始 - 睡眠值是完全随机的,这里用于测试

1 个答案:

答案 0 :(得分:5)

这应解决问题(在胜利10中检查):

 reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d h:\Quotefancy-1542-3840x2160.jpg /f
 Start-Sleep -s 10
 rundll32.exe user32.dll, UpdatePerUserSystemParameters, 0, $false

或者您可以像这样使用win32 api:

$setwallpapersrc = @"
using System.Runtime.InteropServices;
public class wallpaper
{
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 )
{
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
}
}
"@
Add-Type -TypeDefinition $setwallpapersrc
[wallpaper]::SetWallpaper("h:\Quotefancy-1542-3840x2160.jpg")