File.OpenText : File « System.IO « C# / C Sharp by API






File.OpenText

 

using System;
using System.IO;

    public class Exists
    {
        static public void Main (string [] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine ("Please enter a file name");
                return;
            }
            if (!File.Exists (args[0]))
            {
                Console.WriteLine (args[0] + " does not exist");
                return;
            }
            StreamReader reader;
            try
            {
                reader = File.OpenText (args[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine (e.Message);
                Console.WriteLine ("Cannot open " + args[0]);
                return;
            }
            while (reader.Peek() >= 0)
            {
                Console.WriteLine (reader.ReadLine ());
            }
            reader.Close ();
        }
    }

   
  








Related examples in the same category

1.File.CreateText(String fileName)
2.File.Delete
3.File.Exists(String fileName)
4.File.GetLastAccessTime
5.File.OpenRead
6.File.OpenWrite
7.File.ReadAllLines(String fileName)
8.File.SetAccessControl
9.File.WriteAllLines(String fileName, String[] lines)