Thursday, February 14, 2013

Console Application: Read File

Console Application: Read File
Following source code, contains a simple console application that gets a file name as a command line argument and prints its content in the console.

Source Code
using System;
using System.IO;

namespace ReadFile {
      class Program {
            static int Main(string[] args) {
                  if (args.Length == 0) {
                        Console.WriteLine("Please specify filename.");
                        return 0;
                  }
                  if (args.Length > 1) {
                        Console.WriteLine("Too many filenames specified.");
                        return 0;
                  }
                  if(! File.Exists((args[0])))
                        Console.WriteLine("Specify file not found.");
                  Console.WriteLine(File.ReadAllText(args[0]));
                  return 1;
            }
      }
}

Sample Output

Keywords:
Console Application, Command Line Arguments, File I/O

No comments:

Post a Comment