Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions Actions/CloneDisplayAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
using ClassIsland.Core.Abstractions.Automation;
using ClassIsland.Core.Attributes;
using Microsoft.Extensions.Logging;
using SystemTools.Services;

namespace SystemTools.Actions;

/// <summary>
/// 复制屏幕
/// </summary>
[ActionInfo("SystemTools.CloneDisplay", "复制屏幕", "\uE635", false)]
public class CloneDisplayAction(ILogger<CloneDisplayAction> logger) : ActionBase
public class CloneDisplayAction(ILogger<CloneDisplayAction> logger, IProcessRunner processRunner) : ActionBase
{
private readonly ILogger<CloneDisplayAction> _logger = logger;
private readonly IProcessRunner _processRunner = processRunner;

protected override async Task OnInvoke()
{
Expand All @@ -28,26 +27,12 @@ protected override async Task OnInvoke()
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden
};

using var process = Process.Start(processInfo);
if (process != null)
{
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();

await process.WaitForExitAsync();

_logger.LogInformation("复制屏幕命令已执行,退出码: {ExitCode}", process.ExitCode);

if (!string.IsNullOrEmpty(error))
_logger.LogWarning("错误输出: {Error}", error);
}
else
{
throw new Exception("无法启动 DisplaySwitch.exe 进程");
}
await _processRunner.RunAsync(processInfo, "复制屏幕(DisplaySwitch)");
_logger.LogInformation("复制屏幕命令执行完成");
}
catch (Exception ex)
{
Expand All @@ -57,4 +42,4 @@ protected override async Task OnInvoke()

await base.OnInvoke();
}
}
}
56 changes: 20 additions & 36 deletions Actions/CopyAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using SystemTools.Services;
using SystemTools.Settings;

namespace SystemTools.Actions;

[ActionInfo("SystemTools.Copy", "复制", "\uE6AB", false)]
public class CopyAction(ILogger<CopyAction> logger) : ActionBase<CopySettings>
public class CopyAction(ILogger<CopyAction> logger, IProcessRunner processRunner) : ActionBase<CopySettings>
{
private readonly ILogger<CopyAction> _logger = logger;
private readonly IProcessRunner _processRunner = processRunner;

protected override async Task OnInvoke()
{
Expand All @@ -25,16 +27,6 @@ protected override async Task OnInvoke()
return;
}

var psi = new ProcessStartInfo
{
FileName = "cmd.exe",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden
};

try
{
var sourcePath = Settings.SourcePath.TrimEnd('\\');
Expand All @@ -60,16 +52,7 @@ protected override async Task OnInvoke()
Directory.CreateDirectory(destDir);
}

try
{
await Task.Run(() => File.Copy(sourcePath, destPath, true));
}
catch (Exception ex)
{
_logger.LogError(ex, "文件复制失败");
throw new Exception($"复制失败: {ex}");
}

await Task.Run(() => File.Copy(sourcePath, destPath, true));
_logger.LogInformation("文件复制成功: {Source} -> {Destination}", sourcePath, destPath);
}
else
Expand All @@ -93,21 +76,22 @@ protected override async Task OnInvoke()
Directory.Delete(finalDestPath, true);
}

psi.FileName = "robocopy.exe";
psi.Arguments = $"\"{sourcePath}\" \"{finalDestPath}\" /e /copyall /r:3 /w:3 /mt:4 /nfl /ndl /np";
_logger.LogInformation("执行命令: robocopy \"{Source}\" \"{Destination}\"", sourcePath, finalDestPath);

using var process = Process.Start(psi) ?? throw new Exception("无法启动进程");
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();

if (process.ExitCode >= 8)
var psi = new ProcessStartInfo
{
_logger.LogError("robocopy 失败,退出码: {ExitCode}, 输出: {Output}, 错误: {Error}",
process.ExitCode, output, error);
throw new Exception($"robocopy 失败,退出码: {process.ExitCode}");
}
FileName = "robocopy.exe",
Arguments = $"\"{sourcePath}\" \"{finalDestPath}\" /e /copyall /r:3 /w:3 /mt:4 /nfl /ndl /np",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden
};

await _processRunner.RunAsync(
psi,
operationName: "复制文件夹(robocopy)",
successExitCodes: new[] { 0, 1, 2, 3, 4, 5, 6, 7 },
timeout: TimeSpan.FromMinutes(10));

_logger.LogInformation("文件夹复制成功: {Source} -> {Destination}", sourcePath, finalDestPath);
}
Expand All @@ -121,4 +105,4 @@ protected override async Task OnInvoke()
await base.OnInvoke();
_logger.LogDebug("CopyAction OnInvoke 完成");
}
}
}
49 changes: 15 additions & 34 deletions Actions/DeleteAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using SystemTools.Services;
using SystemTools.Settings;

namespace SystemTools.Actions;

[ActionInfo("SystemTools.Delete", "删除", "\uE61D", false)]
public class DeleteAction(ILogger<DeleteAction> logger) : ActionBase<DeleteSettings>
public class DeleteAction(ILogger<DeleteAction> logger, IProcessRunner processRunner) : ActionBase<DeleteSettings>
{
private readonly ILogger<DeleteAction> _logger = logger;
private readonly IProcessRunner _processRunner = processRunner;

protected override async Task OnInvoke()
{
Expand All @@ -24,16 +26,6 @@ protected override async Task OnInvoke()
return;
}

var psi = new ProcessStartInfo
{
FileName = "cmd.exe",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden
};

try
{
var targetPath = Settings.TargetPath.TrimEnd('\\');
Expand All @@ -46,16 +38,7 @@ protected override async Task OnInvoke()
throw new FileNotFoundException("文件不存在", targetPath);
}

try
{
await Task.Run(() => File.Delete(targetPath));
}
catch (Exception ex)
{
_logger.LogError(ex, "文件移动失败");
throw new Exception($"移动失败: {ex}");
}

await Task.Run(() => File.Delete(targetPath));
_logger.LogInformation("文件删除成功: {Path}", targetPath);
}
else
Expand All @@ -66,20 +49,18 @@ protected override async Task OnInvoke()
throw new DirectoryNotFoundException($"文件夹不存在: {targetPath}");
}

psi.Arguments = $"/c rmdir /s /q \"{targetPath}\"";
_logger.LogInformation("执行命令: {Command}", psi.Arguments);

using var process = Process.Start(psi) ?? throw new Exception("无法启动进程");
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();

if (process.ExitCode != 0)
var psi = new ProcessStartInfo
{
_logger.LogError("删除失败,退出码: {ExitCode}, 错误: {Error}", process.ExitCode, error);
throw new Exception($"删除失败: {error}");
}
FileName = "cmd.exe",
Arguments = $"/c rmdir /s /q \"{targetPath}\"",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Hidden
};

await _processRunner.RunAsync(psi, "删除文件夹(rmdir)", timeout: TimeSpan.FromMinutes(10));
_logger.LogInformation("文件夹删除成功: {Path}", targetPath);
}
}
Expand All @@ -92,4 +73,4 @@ protected override async Task OnInvoke()
await base.OnInvoke();
_logger.LogDebug("DeleteAction OnInvoke 完成");
}
}
}
26 changes: 6 additions & 20 deletions Actions/DisableMouseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using ClassIsland.Core.Abstractions.Automation;
using ClassIsland.Core.Attributes;
using Microsoft.Extensions.Logging;
using SystemTools.Services;

namespace SystemTools.Actions;

[ActionInfo("SystemTools.DisableMouse", "禁用鼠标", "\uE5C7", false)]
public class DisableMouseAction(ILogger<DisableMouseAction> logger) : ActionBase
public class DisableMouseAction(ILogger<DisableMouseAction> logger, IProcessRunner processRunner) : ActionBase
{
private readonly ILogger<DisableMouseAction> _logger = logger;
private readonly IProcessRunner _processRunner = processRunner;

protected override async Task OnInvoke()
{
Expand All @@ -23,7 +25,7 @@ protected override async Task OnInvoke()
if (string.IsNullOrEmpty(pluginDir))
{
_logger.LogError("无法获取程序集位置");
throw new FileNotFoundException($"无法获取程序集位置");
throw new FileNotFoundException("无法获取程序集位置");
}

var batchPath = Path.Combine(pluginDir, "jinyongshubiao.bat");
Expand All @@ -34,8 +36,6 @@ protected override async Task OnInvoke()
throw new FileNotFoundException($"找不到禁用鼠标批处理文件: {batchPath}");
}

_logger.LogInformation("正在运行禁用鼠标批处理: {Path}", batchPath);

var psi = new ProcessStartInfo
{
FileName = batchPath,
Expand All @@ -47,21 +47,7 @@ protected override async Task OnInvoke()
WindowStyle = ProcessWindowStyle.Hidden
};

using var process = Process.Start(psi) ?? throw new Exception("无法启动批处理进程");
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();

_logger.LogInformation("禁用鼠标批处理执行完成,退出码: {ExitCode}", process.ExitCode);
if (!string.IsNullOrWhiteSpace(output))
_logger.LogDebug("批处理输出: {Output}", output);
if (!string.IsNullOrWhiteSpace(error))
_logger.LogWarning("批处理错误: {Error}", error);

if (process.ExitCode != 0)
{
_logger.LogWarning("批处理返回非零退出码: {ExitCode}", process.ExitCode);
}
await _processRunner.RunAsync(psi, "禁用鼠标批处理", timeout: TimeSpan.FromMinutes(2));
}
catch (Exception ex)
{
Expand All @@ -72,4 +58,4 @@ protected override async Task OnInvoke()
await base.OnInvoke();
_logger.LogDebug("DisableMouseAction OnInvoke 完成");
}
}
}
26 changes: 6 additions & 20 deletions Actions/EnableMouseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using ClassIsland.Core.Abstractions.Automation;
using ClassIsland.Core.Attributes;
using Microsoft.Extensions.Logging;
using SystemTools.Services;

namespace SystemTools.Actions;

[ActionInfo("SystemTools.EnableMouse", "启用鼠标", "\uE5BF", false)]
public class EnableMouseAction(ILogger<EnableMouseAction> logger) : ActionBase
public class EnableMouseAction(ILogger<EnableMouseAction> logger, IProcessRunner processRunner) : ActionBase
{
private readonly ILogger<EnableMouseAction> _logger = logger;
private readonly IProcessRunner _processRunner = processRunner;

protected override async Task OnInvoke()
{
Expand All @@ -23,7 +25,7 @@ protected override async Task OnInvoke()
if (string.IsNullOrEmpty(pluginDir))
{
_logger.LogError("无法获取程序集位置");
throw new FileNotFoundException($"无法获取程序集位置");
throw new FileNotFoundException("无法获取程序集位置");
}

var batchPath = Path.Combine(pluginDir, "huifu.bat");
Expand All @@ -34,8 +36,6 @@ protected override async Task OnInvoke()
throw new FileNotFoundException($"找不到启用鼠标批处理文件: {batchPath}");
}

_logger.LogInformation("正在运行启用鼠标批处理: {Path}", batchPath);

var psi = new ProcessStartInfo
{
FileName = batchPath,
Expand All @@ -47,21 +47,7 @@ protected override async Task OnInvoke()
WindowStyle = ProcessWindowStyle.Hidden
};

using var process = Process.Start(psi) ?? throw new Exception("无法启动批处理进程");
string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();

_logger.LogInformation("启用鼠标批处理执行完成,退出码: {ExitCode}", process.ExitCode);
if (!string.IsNullOrWhiteSpace(output))
_logger.LogDebug("批处理输出: {Output}", output);
if (!string.IsNullOrWhiteSpace(error))
_logger.LogWarning("批处理错误: {Error}", error);

if (process.ExitCode != 0)
{
_logger.LogWarning("批处理返回非零退出码: {ExitCode}", process.ExitCode);
}
await _processRunner.RunAsync(psi, "启用鼠标批处理", timeout: TimeSpan.FromMinutes(2));
}
catch (Exception ex)
{
Expand All @@ -72,4 +58,4 @@ protected override async Task OnInvoke()
await base.OnInvoke();
_logger.LogDebug("EnableMouseAction OnInvoke 完成");
}
}
}
Loading