C# File Exists

Description

File Exists Determines whether the specified file exists.

Syntax

File.Exists has the following syntax.


public static bool Exists(
  string path
)

Parameters

File.Exists has the following parameters.

  • path - The file to check.

Returns

File.Exists method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.

Example

The following example determines if a file exists.


//from w w  w  . j av  a  2  s. co m
using System;
using System.IO;
public class MainClass{
  public static void Main(String[] argv){  
    string curFile = @"c:\temp\test.txt";
    Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
    
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter