-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVoiceShellCommandRunner.cs
More file actions
43 lines (36 loc) · 1.39 KB
/
VoiceShellCommandRunner.cs
File metadata and controls
43 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Diagnostics;
namespace PrimeDictate;
internal readonly record struct VoiceShellCommandResult(int? ProcessId);
internal static class VoiceShellCommandRunner
{
public static VoiceShellCommandResult Run(VoiceShellCommand? shellCommand)
{
if (shellCommand is null)
{
throw new ArgumentNullException(nameof(shellCommand));
}
var command = shellCommand.Command.Trim();
if (command.Length == 0)
{
throw new InvalidOperationException("Voice command has no command prompt command configured.");
}
var commandProcessor = Environment.GetEnvironmentVariable("ComSpec");
if (string.IsNullOrWhiteSpace(commandProcessor))
{
commandProcessor = "cmd.exe";
}
var startInfo = new ProcessStartInfo
{
FileName = commandProcessor,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
};
startInfo.ArgumentList.Add("/d");
startInfo.ArgumentList.Add("/c");
startInfo.ArgumentList.Add(command);
using var process = Process.Start(startInfo)
?? throw new InvalidOperationException("Windows did not start the command prompt process.");
return new VoiceShellCommandResult(process.Id);
}
}