How do I check if a file is exists?

Category: System.IO, viewed: 2K time(s).
using System;
using System.IO;

namespace Kodecsharp.Example.System.IO {
    class FileExistsDemo {
        public static void Main(string[] args) {
            string fileName = @"c:\users\wsaryada\demo.txt";

            //
            // The File.Exists() method check to see if a file on the
            // specified path exists or not.
            //
            if (File.Exists(fileName)) {
                Console.WriteLine(fileName + " found.");
            } else {
                Console.WriteLine("Could not locate " + fileName);
            }

            Console.ReadLine();
        }
    }
}
Powered by Disqus