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


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

E:
ew.txt

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

if (File.Exists(@"E:
ew.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:
ew.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!

Updated on: 22-Jun-2020

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements