1- using System ;
1+ using ClassIsland . Core . Abstractions . Automation ;
2+ using ClassIsland . Core . Attributes ;
3+ using Microsoft . Extensions . Logging ;
4+ using System ;
25using System . Diagnostics ;
36using System . IO ;
4- using System . Text ;
57using System . Threading . Tasks ;
6- using ClassIsland . Core . Abstractions . Automation ;
7- using ClassIsland . Core . Attributes ;
8- using Microsoft . Extensions . Logging ;
98using SystemTools . Settings ;
109
1110namespace SystemTools . Actions ;
@@ -22,7 +21,9 @@ public ChangeWallpaperAction(ILogger<ChangeWallpaperAction> logger)
2221
2322 protected override async Task OnInvoke ( )
2423 {
25- if ( string . IsNullOrWhiteSpace ( Settings . ImagePath ) )
24+ _logger . LogDebug ( "ChangeWallpaperAction OnInvoke 开始" ) ;
25+
26+ if ( Settings == null || string . IsNullOrWhiteSpace ( Settings . ImagePath ) )
2627 {
2728 _logger . LogWarning ( "图片路径为空" ) ;
2829 return ;
@@ -36,21 +37,13 @@ protected override async Task OnInvoke()
3637
3738 try
3839 {
39- string escapedPath = Settings . ImagePath . Replace ( "\\ " , "\\ \\ " ) ;
40- string script = $@ "
41- Add-Type @'
42- using System;
43- using System.Runtime.InteropServices;
44- public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static extern int SystemParametersInfo(int a, int b, string c, int d);}}
45- '@;
46- [W]::SystemParametersInfo(20, 0, ""{ escapedPath } "", 0x01)" . Trim ( ) ;
47-
48- string encodedScript = Convert . ToBase64String ( Encoding . Unicode . GetBytes ( script ) ) ;
40+ var imagePath = Settings . ImagePath ;
41+ _logger . LogInformation ( "正在切换壁纸到: {Path}" , imagePath ) ;
4942
5043 var psi = new ProcessStartInfo
5144 {
5245 FileName = "powershell.exe" ,
53- Arguments = $ "-NoProfile -EncodedCommand { encodedScript } ",
46+ Arguments = $ "-NoProfile -Command \" Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class W {{ [DllImport( \\ \" user32.dll \\ \" , CharSet = CharSet.Auto)] public static extern int SystemParametersInfo(int a, int b, string c, int d); }}' -Language CSharp; [W]::SystemParametersInfo(20, 0, ' { imagePath . Replace ( "'" , "''" ) } ', 0x01) \" ",
5447 CreateNoWindow = true ,
5548 UseShellExecute = false ,
5649 RedirectStandardOutput = true ,
@@ -59,25 +52,27 @@ public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static e
5952 } ;
6053
6154 using var process = Process . Start ( psi ) ;
62- if ( process ! = null )
55+ if ( process = = null )
6356 {
64- string output = await process . StandardOutput . ReadToEndAsync ( ) ;
65- string error = await process . StandardError . ReadToEndAsync ( ) ;
66- await process . WaitForExitAsync ( ) ;
57+ throw new Exception ( "无法启动 PowerShell 进程" ) ;
58+ }
6759
68- if ( ! string . IsNullOrEmpty ( error ) && ! error . TrimStart ( ) . StartsWith ( "#< CLIXML" ) )
69- {
70- _logger . LogWarning ( "PowerShell 错误: {Error}" , error ) ;
71- }
60+ string output = await process . StandardOutput . ReadToEndAsync ( ) ;
61+ string error = await process . StandardError . ReadToEndAsync ( ) ;
62+ await process . WaitForExitAsync ( ) ;
7263
73- if ( process . ExitCode == 0 )
74- _logger . LogInformation ( "壁纸切换成功: {Path}" , Settings . ImagePath ) ;
75- else
76- _logger . LogWarning ( "壁纸切换可能失败,退出码: {ExitCode}" , process . ExitCode ) ;
64+ if ( ! string . IsNullOrEmpty ( error ) && ! error . TrimStart ( ) . StartsWith ( "#< CLIXML" ) )
65+ {
66+ _logger . LogWarning ( "PowerShell 错误: {Error}" , error ) ;
67+ }
68+
69+ if ( process . ExitCode == 0 )
70+ {
71+ _logger . LogInformation ( "壁纸切换成功: {Path}" , imagePath ) ;
7772 }
7873 else
7974 {
80- throw new Exception ( "无法启动 PowerShell 进程" ) ;
75+ _logger . LogWarning ( "壁纸切换可能失败,退出码: {ExitCode}" , process . ExitCode ) ;
8176 }
8277 }
8378 catch ( Exception ex )
@@ -87,5 +82,6 @@ public class W{{[DllImport(""user32.dll"", CharSet=CharSet.Auto)]public static e
8782 }
8883
8984 await base . OnInvoke ( ) ;
85+ _logger . LogDebug ( "ChangeWallpaperAction OnInvoke 完成" ) ;
9086 }
9187}
0 commit comments