Show / Hide Table of Contents

Class consoleProcess

Runs a console program in hidden mode. Gets its output text and can write input text.

public sealed class consoleProcess : IDisposable
Remarks

Must be disposed. In the example the using statement does it.

Examples
using var c = new consoleProcess(folders.Workspace + @"exe\console1\console1.exe");
//c.Encoding = Console.OutputEncoding;
while (c.Read(out var s)) {
	if (c.IsLine) {
		print.it($"<><c green><_>{s}</_><>");
	} else {
		if (s == "User: ") c.Write("A");
		else if (s == "Password: ") c.Write("B");
		//else if (c.Wait()) continue; //let next Read wait for more text and get old + new text. Use this if other prompts are not possible.
		else if (c.Wait(500)) continue; //wait for more text max 500 ms. If received, let next Read get old + new text.
		else if (dialog.showInput(out var s1, null, s)) c.Write(s1);
		//else print.it($"<><c blue><_>{s}</_><><nonl>");
		else throw new OperationCanceledException();
	}
}
if (c.ExitCode is int ec && ec != 0) throw new Exception($"Failed. Exit code: {ec}");
using var c = new consoleProcess("example.exe");
c.Prompt("User: ", "A");
c.Prompt("Password: ", "B");
while (c.Read(out var s)) print.it(s);
print.it(c.ExitCode);

Namespace: Au
Assembly: Au.dll
Inheritance
object
consoleProcess

Constructors

Name Description
consoleProcess(string, string, string)

Starts the console program.

Properties

Name Description
Encoding

Console's text encoding. Default is System.Text.Encoding.UTF8.

Ended

Returns true if a ReadX function detected that the console output stream is closed. The process is ended or ending.

ExitCode

Gets the exit code of the console process. If the process is still running, waits until it exits.

InputEncoding

Input text encoding for consoleProcess.Write. If null (default), will use consoleProcess.Encoding.

IsLine

consoleProcess.Read sets this property = true if in console output the line text ended with newline characters; false if not.

TerminateFinally

If the console process is still running when this variable is dying, terminate it. Default true.

Methods

Name Description
Dispose()
Prompt(string, string)

Waits for next prompt (incomplete line that asks for user input). Reads the prompt and all lines before it. Then can write input text and "\n".

Read(out string)

Waits and reads next full or partial line.

ReadAllText()

Reads all console output text until its process ends. Returns that text.

ReadAllText(Action<string>)

Reads all console output text until its process ends. Calls callback function.

ReadLine(out string)

Waits and reads next full line.

TerminateNow(int)

Terminates the console process.

Wait(int)

Waits for more text and tells next consoleProcess.Read to get old + new text.

Write(string, bool)

Sends text to the console's input. Also sends character '\n' (like key Enter), unless text ends with '\n' or noNL is true.

See Also

run.console