Skip to content

[API Proposal]: ProcessStartInfo redirected to null device #129656

@ladeak

Description

@ladeak

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Diagnostics.ProcessuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions