7

I'd like my app to read from files specified by command-line argument or from standard in, so the user can use it myprogram.exe data.txt or otherprogram.exe | myprogram.exe. How can I do this in C#?


In Python, I'd write

import fileinput
for line in fileinput.input():
    process(line)

This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin.

Perl's <> and Ruby's ARGF are similarly useful .

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • static void Main(string[] args), and read the args array? – Thomas Oct 07 '12 at 18:05
  • Some suggestions here, too: http://stackoverflow.com/questions/3961542/checking-standard-input-in-c-sharp – dash Oct 07 '12 at 18:08
  • Related posts - [How to use a txt file as command line argument?](https://stackoverflow.com/q/12770561/465053) & [Debugging with command-line parameters in Visual Studio](https://stackoverflow.com/q/298708/465053) – RBT May 24 '18 at 10:08

4 Answers4

10

stdin is exposed to you as a TextReader through Console.In. Just declare a TextReader variable for your input that either uses Console.In or the file of your choosing and use that for all your input operations.

static TextReader input = Console.In;
static void Main(string[] args)
{
    if (args.Any())
    {
        var path = args[0];
        if (File.Exists(path))
        {
            input = File.OpenText(path);
        }
    }

    // use `input` for all input operations
    for (string line; (line = input.ReadLine()) != null; )
    {
        Console.WriteLine(line);
    }
}

Otherwise if refactoring to use this new variable would be too expensive, you could always redirect Console.In to your file using Console.SetIn().

static void Main(string[] args)
{
    if (args.Any())
    {
        var path = args[0];
        if (File.Exists(path))
        {
            Console.SetIn(File.OpenText(path));
        }
    }

    // Just use the console like normal
    for (string line; (line = Console.ReadLine()) != null; )
    {
        Console.WriteLine(line);
    }
}
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
3

That's awfully easy, actually.

In the C# code editor, you can do:

public static void Main(string[] args) {
    //And then you open up a file. 
    using(Streamreader sr = new Streamreader(args[0])) {
            String line = sr.ReadToEnd();
            Console.WriteLine(line);
    }
}

Another good idea would be to iterate over the items args in a c# collection, so that you can take multiple files as input. Example: main.exe file1.txt file2.txt file3.txt and so on.

You'd do that by modifying the above code using a special for loop, like follows:

foreach(string s in args) {
    using( Streamreader sr = new Streamreader(s) ) {
        String line = sr.ReadToEnd();
        Console.WriteLine(line);
    }
}

Good luck!

alvonellos
  • 1,009
  • 1
  • 9
  • 27
0

use

 static void Main(string[] args)

and then iterate over each input with args.length in a for-loop for example.

example of use: http://www.dotnetperls.com/main

Thomas
  • 1,563
  • 3
  • 17
  • 37
-1

Try this:

public void Main(string[] args)
        {
            if (args.Count() > 0)
            {

                byte[] byteArray = Encoding.UTF8.GetBytes(args[0]);
                MemoryStream stream = new MemoryStream(byteArray);

                StreamReader sr = new StreamReader(stream);
                String line = sr.ReadToEnd();
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }

args[0] is a string which must be converted to a stream before passing to StreamReader constructor.