user2855862
user2855862

Reputation: 11

Read a File with C# from command-line

How I read a file from a command-line in C#? An example command is:

program.exe < textfile

Upvotes: 1

Views: 479

Answers (3)

David
David

Reputation: 219016

That is sending the file to standard input, so you can get the data stream through the Console.In property.

Here's a quick proof of concept:

public static void Main(string[] args)
{
    var fileContents = System.Console.In.ReadToEnd();
    System.Console.Write(fileContents);
}

This was called on the command prompt with:

program.exe < file.txt

Upvotes: 2

Chris Dunaway
Chris Dunaway

Reputation: 11216

As pointed out by P0W, that is a function of cmd.exe, not C#. However, in C#, you would use the various Console.ReadXXX methods to get data from standard input.

Upvotes: 0

P0W
P0W

Reputation: 47824

Input/Output re-direction works for all executable independent of language.

So just read the file in normal way from standard input

Upvotes: 0

Related Questions