How to check the Existence of a File using C#?

CsharpProgrammingServer Side Programming

Let’s say we need to find the following file −

E:\new.txt

To check the existence of the above file, use the Exists() method −

if (File.Exists(@"E:\new.txt")) {
   Console.WriteLine("File exists...");
}

Here is the complete code to check the existence of a file −

Example

 Live Demo

using System;
using System.IO;

public class Demo {
   public static void Main() {
      if (File.Exists(@"E:\new.txt")) {
         Console.WriteLine("File exists...");
      } else {
         Console.WriteLine("File does not exist in the E directory!");
      }
   }
}

Output

File does not exist in the E directory!
raja
Published on 31-Aug-2018 11:28:04
Advertisements