夜雨聆风学习资料网

ARTICLE · 1524859

分享一个每次登录电脑会自动更换桌面背景的小程序。

分享一个每次登录电脑会自动更换桌面背景的小程序。
是个简单的脚本程序,用于更换桌面背景。图片来源于你指定的文件夹,程序会按顺序循环调用指定文件夹内的图片,文件夹内的图片可以随时删减或添加。图片格式限定于:"*.jpg","*.jpeg","*.png","*.webp"。
此程序只适合于windows系统。
起因:
1、常年旅行所拍照片很少有动机去主动翻看,这个小程序可以不经意间让你回看过往。
2、随着年龄渐老,记忆力会明显衰退。它可以随时帮你与过往重建链接,锻炼和维持你的记忆力。
以下是程序的设置过程:

🪟 Windows(PowerShell + 任务计划程序)

一. 准备脚本  C:\Scripts\DailyWallpaper.ps1,

(复制以下有底色代码,粘贴到一个新建NOTE记事本文件中,设置图片文件夹路径,然后保存为C:\Scripts\DailyWallpaper.ps1 )

$Folder    = "E:\screenwall"   # ← 改成你的旅拍文件夹

$StateFile = "$env:USERPROFILE\AppData\Local\wallpaper_index.txt"

$Exts      = @("*.jpg","*.jpeg","*.png","*.webp")

$LogFile   = "$env:TEMP\wallpaper_debug.log"

# 1. 获取图片列表(按文件名排序)

$imgs = @()

foreach ($e in $Exts) {

    $imgs += Get-ChildItem $Folder -Filter $e -File -ErrorAction SilentlyContinue

}

$imgs = $imgs | Sort-Object Name

# 2. 文件夹为空则退出

if ($imgs.Count -eq 0) {

    Add-Content $LogFile "$(Get-Date) No images, skip"

    exit 0

}

# 3. 读取索引

$idx = -1

if (Test-Path $StateFile) {

    $raw = Get-Content $StateFile -ErrorAction SilentlyContinue

    if ($raw -match '^-?\d+$') {

        $idx = [int]$raw

    }

}

# 4. 索引越界保护(删图/文件损坏)

if ($idx -lt -1 -or $idx -ge $imgs.Count) {

    $idx = -1

}

# 5. 下一张

$idx = ($idx + 1) % $imgs.Count

# 6. 二次兜底

if ($null -eq $imgs[$idx]) {

    $idx = 0

}

$chosen = $imgs[$idx]

# 7. 保存状态

$idx | Out-File $StateFile -NoNewline -Force

# 8. 设置壁纸(✅ 正确包裹的 C# 代码块)

Add-Type -TypeDefinition @"

using System.Runtime.InteropServices;

public class Wallpaper {

    [DllImport("user32.dll", SetLastError = true)]

    public static extern int SystemParametersInfo(

        int uAction,

        int uParam,

        string lpvParam,

        int fuWinIni);

}

"@

[Wallpaper]::SystemParametersInfo(20, 0, $chosen.FullName, 0x01 -bor 0x02)

# 9. 日志(用英文,规避编码问题)

Add-Content $LogFile "$(Get-Date) Set wallpaper: $($chosen.Name) (idx=$idx / $($imgs.Count))"

二. 任务计划程序

• Win+R → taskschd.msc

• 创建基本任务 → 触发器 → 登录时

• 操作 → 启动程序:powershell.exe

• 参数:-WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Scripts\DailyWallpaper.ps1"

• 属性 → 常规勾“使用最高权限运行”;条件里取消“只有在交流电源下才运行”(笔记本必做)

首次手动跑一次验证:

Win+R → 

powershell -ExecutionPolicy Bypass -File C:\Scripts\DailyWallpaper.ps1

——————————————————————————————
天藏,2026-8

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料