Check If File Exists In C#

This snippet checks to see if a file exists using the FileInfo Class.


  1. using System;
  2. using System.IO;
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         FileInfo file = new FileInfo("file.txt");
  8.         if ( file.Exists ){
  9.             Console.Write("File Exists");
  10.         }else{
  11.             Console.Write("File Does Not Exist");
  12.         }
  13.         Console.ReadKey();
  14.     }
  15. }

No comments:

Post a Comment