Introduction
The File.Exists method checks if a file exists in C# at a specified location. The File class is defined in the System.IO namespace. If the File.Exists method returns true; the file exists, and the else file does not exist.
The following example demonstrates how to use the File class to check whether a file exists in the File class, and depending on the result, either create a new file and write to it or open the existing file and read from it. Note: Before you run this code, make sure you create a c:\temp folder on your machine. OR change this folder to where you want to create a new file.
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MySample.txt";
if (!File.Exists(path))
{
// Create a file to write to
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}
Directory.Exists(String) Method
The Directory.Exists checks whether the given path refers to an existing directory on disk.
Syntax
public static bool Exists (string path);
Example
This example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach (string path in args)
{
if (File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if (Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
ProcessFile(fileName);
}
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
ProcessDirectory(subdirectory);
}
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
We can use another process to potentially do something with the file between the time you call the Exists method and perform another operation on the file, such as Delete.
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
Directory.Get Current Directory Method
This method gets the current working directory of the application.
public static string GetCurrentDirectory()

o abuzaidPosted Oct 19, 2020, 9:32 AM
It works with me , thanks
Kevin WasiePosted May 13, 2020, 8:58 PM
Your code says File.sample instead of File.Exists.
Dharmendra Kumar PanditPosted Nov 27, 2019, 12:24 AM
Nice One ....
Gopal MahalePosted May 4, 2015, 10:21 AM
its not working properly... it always return false