Windows更换壁纸脚本 获取必应壁纸 定时更换壁纸 c#调用.ps1脚本

想给电脑换上必应的每日壁纸,之前使用的是Bing Wallpaper软件,这个软件每日刷新只能更换为前一天的壁
纸,而且需要自启动更换壁纸,于是打算用定时计划powershell脚本完成这个工作。

1.解除PowerShell限制

.ps1文件类似.bat文件,是PowerShell写好的脚本文件。
在Windows系统中,默认情况下是不允许执行.ps1文件的。
PowerShell中执行如下命令

Set-ExecutionPolicy Unrestricted

执行完毕之后就能运行.ps1脚本了。

2.更换壁纸方法

function Set-Wallpaper($image) {
    $source = @"
  using System;
  using System.Runtime.InteropServices;
    
  public class Params
  {
      [DllImport("User32.dll",CharSet=CharSet.Unicode)]
      public static extern int SystemParametersInfo (Int32 uAction,
                                                      Int32 uParam,
                                                      String lpvParam,
                                                      Int32 fuWinIni);
  }
"@
    
    Add-Type -TypeDefinition $source
    
    $SPI_SETDESKWALLPAPER = 0x0014
    $UpdateIniFile = 0x01
    $SendChangeEvent = 0x02
    
    $fWinIni = $UpdateIniFile -bor $SendChangeEvent
    
    $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}
Set-WallPaper -image "壁纸路径"

将"壁纸路径"更换为要设置的壁纸路径,保存代码至.ps1文件执行即可更换壁纸

3.自动执行

使用计划任务可以自动执行脚本
打开 控制面板 - 系统和安全 - 管理工具 - 计划任务
打开后点击 创建基本任务
根据向导设置任务参数
在这里插入图片描述
其中启动程序中的程序或脚本设置为 powershell 添加参数(可选) 设置为刚刚保存好的.ps1文件

4.获取必应每日壁纸并设置

更改代码如下

function Save-BingTodayImage()
{
    #必应图片故事API
    $bingImageApi ='http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=zh-cn'
    $bingUri = 'http://www.bing.com/'
 
    # 获取图片链接
    [xml]$bingImageXml = (Invoke-WebRequest -Uri $bingImageApi).Content
    Write-Host " 今日图片故事: $( $bingImageXml.images.image.copyright ) "
    $imgLink = '{0}{1}' -f $bingUri , $bingImageXml.images.image.url
 
    # 下载和保存图片
    $imageDir = "$HOME\Pictures\Bing\"

    if( -not (Test-Path $imageDir) )
    {
        mkdir $imageDir | Out-Null
    }
    else
    {
        # 删除过期图片
        Remove-Item ${imageDir}*
    }

    $imageFile = Join-Path $imageDir ( '{0}.jpg' -f $bingImageXml.images.image.fullstartdate)
 
    Invoke-WebRequest -Uri $imgLink -OutFile $imageFile

    return $imageFile
}
 
function Set-DesktopWallPaper($imagePath)
{
    Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $imagePath
    RUNDLL32.EXE USER32.DLL UpdatePerUserSystemParameters ,1 ,True
}

# 获取今日必应背景图片
$image=Save-BingTodayImage
 
#设置为桌面墙纸
Set-DesktopWallPaper -imagePath $image

大功告成!


补充

按照以上方式执行定时计划时,每次都会弹出powershell窗口,查了很多方法也不能像类似pythonw.exe方式静默(不打开窗口)执行ps1脚本,于是想到之前写的c#调用Windows脚本的方法。

主要思路

使用c#中的process类调用powershell,将StartInfo.CreateNoWindow设置为true,即不打开窗口执行程序,亲测有效!

c#代码

新建c#控制台项目,为防止产生运行黑框,右键项目属性输出类型设置为Windows应用程序
在这里插入图片描述

static void Main(string[] args)
{
    ExecutePowerShell();
}

static void ExecutePowerShell()
{
    Process pro = new Process();
    pro.StartInfo.FileName = "powershell";
    pro.StartInfo.RedirectStandardInput = true;
    pro.StartInfo.UseShellExecute = false;
    pro.StartInfo.CreateNoWindow = true;
    string path = @""; //ps1脚本文件位置,自行修改
    pro.Start();
    pro.StandardInput.WriteLine(path);
    pro.StandardInput.WriteLine("exit"); //执行完退出powershell
    pro.StandardInput.AutoFlush = true;
    pro.WaitForExit();
    pro.Close();
}

代码写好后发布到文件夹。

修改任务计划参数

需管理员权限执行
在这里插入图片描述

修改运行程序,由于c#发布后是exe可执行文件,直接将要运行的程序改为发布的exe文件即可。
在这里插入图片描述

设置完成后每次执行任务时就不会弹出窗口了!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值