File class to check whether a file exists, open and read

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// cp.cs -- Uses methods in the File class to check whether a file exists.
//          If it exists, it then opens and reads the file to the console.
//
//          Compile this program with the following command line
//              C:>csc cp.cs
using System;
using System.IO;

namespace nsStreams
{
    public class cp
    {
        static public void Main (string [] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine ("usage: cp <copy from> <copy to>");
                return;
            }
            if (!File.Exists (args[0]))
            {
                Console.WriteLine (args[0] + " does not exist");
                return;
            }
            bool bOverwrite = false;
            if (File.Exists (args[1]))
            {
                Console.Write (args[1] + " already exists. Overwrite [Y/N]? ");
                string reply = Console.ReadLine ();
                char ch = (char) (reply[0] &amp; (char) 0xdf);
                if (ch != &#039;Y&#039;)
                    return;
                bOverwrite = true;
            }
            File.Copy (args[0], args[1], bOverwrite);
        }
    }
}



           
          


This entry was posted in File Stream. Bookmark the permalink.