Background and motivation
As an extension to proposal #128453 a one-liner to redirect all stdin/out/error to null device.
Today it is possible by either setting the bool silent parameter on Process.Run and Process.RunAsync methods or by setting all outputs one-by-one on ProcessStartInfo. (see the linked issues)
using SafeFileHandle nullHandle = File.OpenNullHandle();
ProcessStartInfo psi = new("exeName", ["--args"])
{
StandardInputHandle = nullHandle,
StandardOutputHandle = nullHandle,
StandardErrorHandle = nullHandle
};
Process.Run(psi);
Or
public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
However, when using ProcessStartInfo the null-handle needs to be repeated times, and no overload with a combination of PSI and silent parameters on the Run/RunAsync methods.
As briefly discussed in the API review of #128453, we cannot add a new property to ProcessStartInfo that would set all 3 handles to null device, but a method could work.
API Proposal
Sets all handles to null handle:
namespace System.Diagnostics;
public sealed partial class ProcessStartInfo
{
+ public void SetSiletHandles();
}
API Usage
ProcessStartInfo psi = new("exeName", ["--args"]);
psi.SetSiletHandles();
Not sure if the method should return void or ProcessStartInfo, because with the latter it could:
ProcessStartInfo psi = new("exeName", ["--args"]).SetSiletHandles();
Alternative Designs
I proposed SetSilentHandles() method name to match the bool silent parameters on the Run/RunAsync methods. However, these names might be better fitting: SetNullHandles() or DiscardOutputs() or Silence() or WithNullHandles()
For example, given this usage:
ProcessStartInfo psi = new("exeName", ["--args"]).WithNullHandles();
Risks
It is a convenience API.
Background and motivation
As an extension to proposal #128453 a one-liner to redirect all stdin/out/error to null device.
Today it is possible by either setting the
bool silentparameter onProcess.RunandProcess.RunAsyncmethods or by setting all outputs one-by-one onProcessStartInfo. (see the linked issues)Or
However, when using
ProcessStartInfothe null-handle needs to be repeated times, and no overload with a combination of PSI and silent parameters on the Run/RunAsync methods.As briefly discussed in the API review of #128453, we cannot add a new property to
ProcessStartInfothat would set all 3 handles to null device, but a method could work.API Proposal
Sets all handles to null handle:
namespace System.Diagnostics; public sealed partial class ProcessStartInfo { + public void SetSiletHandles(); }API Usage
Not sure if the method should return
voidorProcessStartInfo, because with the latter it could:Alternative Designs
I proposed
SetSilentHandles()method name to match thebool silentparameters on the Run/RunAsync methods. However, these names might be better fitting:SetNullHandles()orDiscardOutputs()orSilence()orWithNullHandles()For example, given this usage:
Risks
It is a convenience API.