Opening a text file is passed as a command line parameter [closed]

A starting point. Then what you want to do with the contents of the file is up to you

using System.IO;    // <- required for File and StreamReader classes

static void Main(string[] args)
{
    if(args != null && args.Length > 0)
    {
        if(File.Exists(args[0]))
        {
            using(StreamReader sr = new StreamReader(args[0]))
            {
                string line = sr.ReadLine();
                ........
            }
        }
    }
}

the above approach reads one line at a time to process the minimal quantity of text, however, if the file size is not a concert you could avoid the StreamReader object and use

        if(File.Exists(args[0]))
        {
            string[] lines = File.ReadAllLines(args[0]);
            foreach(string line in lines)
            {
                 ... process the current line
            }
        }

Leave a Comment