DotnetStandardStreams/readme.md
2025-05-15 13:27:49 -06:00

2.2 KiB

DotnetStreams

Read text from STDIN regardless of the source.

Description

This library lets you read text input from STDIN that is entered from the console, piped to the application on the command line without any knowledge of the method. This is useful because you would normally not know if the stream has ended or not and have to check for it differently depending on the source.

Consuming the Library

You can use the library in several ways:

  • By copying the code to your source repo and adding a project reference to the DotnetStreams project.
  • By adding a NuGet folder resource to your NuGet sources configuration which points to this project.
  • By adding a NuGet reference to the DotnetStreams project in the repo at nuget.pillidar.com.

How To Configure NuGet Sources

In Visual Studio 2022, right-click the Dependencies node in the Solution Explorer for your project. Click Manage NuGet Packages. In the upper-right corner, click the cog icon next to the Package Source dropdown.

Adding a NuGet Folder Resource

Add a new Package Source.

Name DotnetStreams Folder
Source C:\dev\DotnetStreams\source\DotnetStreams\bin\Release

Adding a NuGet Repo Reference to nuget.pillidar.com

Add a new Package Source.

Name pillidar.com
Source https://nuget.pillidar.com/v3/index.json

Adding a NuGet repo at nuget.pillidar.com

Usage Instructions

Consuming Application

Your console app can use DotnetStreams to enable usage such as these:

echo "This is a test." | myconsoleapp.exe
type myfile.txt | myconsoleapp.exe
c:> myconsoleapp.exe
line 1
line 2

line 3
^Z

Code Usage

static void Main(string[] args)
{
	ITextSource textSource;
	IOutputTarget textTarget = new ConsoleOutputTarget();

	if (args.Length > 0)
		textSource = new FileTextSource(args[0]);
	else
		textSource = new StdInTextSource();

	Execute(
		textSource,
		textTarget);
}

public static void Execute(ITextSource source, IOutputTarget target)
{
	source.Open();
	target.Open();

	foreach (string line in source.ReadAll())
		target.Output(line);

	target.Close();
	source.Close();
}