MaiTouchSensorEmulator/VirtualComPortManager.cs

62 lines
1.7 KiB
C#
Raw Normal View History

2024-02-08 18:02:54 +08:00
using System.Diagnostics;
using System.Text;
2024-02-08 18:02:54 +08:00
namespace WpfMaiTouchEmulator;
internal class VirtualComPortManager
{
2024-02-08 18:02:54 +08:00
public Task<string> CheckInstalledPortsAsync()
{
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"list");
2024-02-08 18:02:54 +08:00
}
2024-02-08 18:02:54 +08:00
public Task<string> InstallComPort()
{
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"install PortName=COM3 PortName=COM23");
2024-02-08 18:02:54 +08:00
}
2024-02-08 18:02:54 +08:00
public Task<string> UninstallVirtualPorts()
{
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"uninstall");
2024-02-08 18:02:54 +08:00
}
2024-02-08 18:02:54 +08:00
private async Task<string> ExecuteCommandAsync(string command, string arguments)
{
var processStartInfo = new ProcessStartInfo
{
2024-02-08 18:02:54 +08:00
FileName = command,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = "thirdparty programs\\com0com"
2024-02-08 18:02:54 +08:00
};
var outputBuilder = new StringBuilder();
using var process = new Process { StartInfo = processStartInfo };
process.OutputDataReceived += (sender, args) =>
{
if (args.Data != null)
{
2024-02-08 18:02:54 +08:00
outputBuilder.AppendLine(args.Data);
}
};
process.ErrorDataReceived += (sender, args) =>
{
if (args.Data != null)
{
2024-02-08 18:02:54 +08:00
outputBuilder.AppendLine(args.Data);
}
};
2024-02-08 18:02:54 +08:00
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
2024-02-08 18:02:54 +08:00
await process.WaitForExitAsync();
2024-02-08 18:02:54 +08:00
return outputBuilder.ToString();
}
}