Error Info: index (zero based) must be greater than or equal to zero and less than the size of the argument list.
using System;
//using System.Collections.Generic;
using System.Text;
using System.Reflection;
// This is for the StreamReader class.
using System.IO;
namespace FileExample2
{
class Program
{
static void Main(string[] args)
{
//// Verify that the user entered a filename on the command line.
if (args.Length < 1)
{
string programName =
Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]);
string usageMsg = string.Format("USAGE: {0} <filename>\n", programName);
Console.WriteLine(usageMsg);
}
else
{
// The StreamReader class lets us read from a text file.
// initalized stream to NULL in case the instanticate of the stream fail.
StreamReader stream = null;
try
{
// Instantiating the StreamReader object opens the file.
// Note that an exception is thrown if the file cannot be opened--hence
// the try/catch block!
//
// args[0] is presumably the filename supplied on the command line.
stream = new StreamReader( args[0] );
// Let's read an entire line from the file at a time and, using the comma
// as a delimiter, report how many comma-delimited values are on each line.
//
// Let's also change this loop into a 'for' loop.
int lineNum = 1;
while(stream.Peek() > 0)
{
string lineFromFile = stream.ReadLine();
string[] values = lineFromFile.Split(new char[] { ',' });
Console.WriteLine("Line {0,3:000} has {1,-2} values: {3}", lineNum, values.Length,lineFromFile);
++lineNum;
}
}
catch (Exception ex)
{
Console.WriteLine("Problem reading file: {0}", ex.Message);
}
finally
{
// Try not to forget to close the file!! Don't wait for program exit to do it for you.
// Always do it in a timely manner because it's a shared resource--another program
// might be waiting to use it.
stream.Close();
}
}
}
}
}