Class ProcessRunner
- Namespace
- SharpConsoleUI.Helpers
- Assembly
- SharpConsoleUI.dll
Safe process execution helper that prevents command injection by always using ArgumentList (never string-concatenated arguments). All process spawning in the framework should go through this class.
public static class ProcessRunner
- Inheritance
-
ProcessRunner
- Inherited Members
Methods
CanRun(string, IEnumerable<string>, int)
Checks if a given executable can be found and run (exits without error).
public static bool CanRun(string executable, IEnumerable<string> arguments, int timeoutMs = 10000)
Parameters
executablestringThe executable name or path.
argumentsIEnumerable<string>Arguments for the probe invocation.
timeoutMsintMaximum time to wait.
Returns
- bool
True if the process started and exited successfully.
RunAndReadOutput(string, IEnumerable<string>, int)
Runs a process with the given arguments and returns its stdout content. Arguments are passed individually via ArgumentList to prevent injection.
public static string RunAndReadOutput(string executable, IEnumerable<string> arguments, int timeoutMs = 10000)
Parameters
executablestringThe executable name or path.
argumentsIEnumerable<string>Individual arguments (not shell-joined).
timeoutMsintMaximum time to wait for the process to exit.
Returns
- string
The process stdout output, or empty string on failure.
RunWithInput(string, IEnumerable<string>, string, int)
Runs a process with the given arguments, writes input to stdin, and waits for exit.
public static void RunWithInput(string executable, IEnumerable<string> arguments, string stdinInput, int timeoutMs = 10000)
Parameters
executablestringThe executable name or path.
argumentsIEnumerable<string>Individual arguments (not shell-joined).
stdinInputstringContent to write to the process stdin.
timeoutMsintMaximum time to wait for the process to exit.
StartProcess(string, IEnumerable<string>, bool, bool, bool)
Starts a long-running process (e.g., ffmpeg) and returns it for stream-based I/O. The caller is responsible for disposing the process.
public static Process? StartProcess(string executable, IEnumerable<string> arguments, bool redirectStdin = false, bool redirectStdout = false, bool redirectStderr = false)
Parameters
executablestringThe executable name or path.
argumentsIEnumerable<string>Individual arguments.
redirectStdinboolWhether to redirect stdin.
redirectStdoutboolWhether to redirect stdout.
redirectStderrboolWhether to redirect stderr.
Returns
- Process
The started process, or null if it failed to start.