File.Exists Method

Determines whether the specified file exists.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Public Shared Function Exists ( _
	path As String _
) As Boolean
Dim path As String
Dim returnValue As Boolean

returnValue = File.Exists(path)
public static boolean Exists (
	String path
public static function Exists (
	path : String
) : boolean



The file to check.

Return Value

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 a null reference (Nothing in Visual Basic), 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.

The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Existsl returns false.

Be aware that another process can potentially do something with the file in between the time you call the Exists method and perform another operation on the file, such as Delete. A recommended programming practice is to wrap the Exists method, and the operations you take on the file, in a try...catch block as shown in the example. This helps to narrow the scope for potential conflicts. The Exists method can only help to ensure that the file will be available, it cannot guarantee it.

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.

If path describes a directory, this method returns false. Trailing spaces are removed from the path parameter before determining if the file exists.

The following example uses the Exists method to help ensure that a file is not overwritten.

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim path2 As String = path + "temp"
            Dim sw As StreamWriter = File.CreateText(path)
            ' Do the Copy operation only if the first file exists
            ' and the second file does not.
            If File.Exists(path) Then
                If File.Exists(path2) Then
                    Console.WriteLine("The target file already exists.")
                    'try to copy it
                    File.Copy(path, path2)
                    Console.WriteLine("{0} was copied to {1}.", path, path2)
                End If
                Console.WriteLine("The source file does not exist.")
            End If
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

import System.*;
import System.IO.*;

class Test
    public static void main(String[] args)
        String path = "c:\\temp\\MyTest.txt";
        String path2 = path + "temp";

        try {            
            StreamWriter sw = File.CreateText(path);
            try {
            finally {

            // Only do the Copy operation if the first file exists
            // and the second file does not.
            if (File.Exists(path)) {
                if (File.Exists(path2)) {
                    Console.WriteLine("The target already exists");
                else {
                    // Try to copy the file.
                    File.Copy(path, path2);
                    Console.WriteLine("{0} was copied to {1}.", path, path2);
            else {
                Console.WriteLine("The source file does not exist.");
        catch (System.Exception exp) {
            Console.WriteLine("Double copying is not allowed, as expected.");
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0