Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileInfo.Exists Property

Gets a value indicating whether a file exists.

[Visual Basic]
Overrides Public ReadOnly Property Exists As Boolean
[C#]
public override bool Exists {get;}
[C++]
public: __property bool get_Exists();
[JScript]
public override function get Exists() : Boolean;

Property Value

true if the file exists; false if the file does not exist or if the file is a directory.

Remarks

For an example of using this property, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Copy a file. File.Copy

FileInfo.CopyTo

Rename or move a file. File.Move

FileInfo.MoveTo

Get the size of a file. Length
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
Sort files in a directory by size. GetFileSystemInfos
Set file attributes. SetAttributes

Example

The following example opens or creates a file, closes it, deletes it, and then uses the Exists property to ensure that the file has been deleted.

[Visual Basic] 
Imports System
Imports System.IO

Public Class ExistsTest

    Public Shared Sub Main()
        Dim neFile As String = "nonexistentfile"
        Dim fi As New FileInfo(neFile)
        ' Open an existing file, or create a new one.
        DetermineExists(fi, neFile)
        neFile = "newFile.txt"
        ' Create the file on disk.
        fi = New FileInfo(neFile)
        Dim fs As FileStream = fi.Create()
        DetermineExists(fi, neFile)
        ' Close the file so that it can be deleted.
        fs.Close()
        ' Delete the file.
        Try
            fi.Delete()
            Console.WriteLine("The file '{0}' was deleted successfully.", fi.Name)
        Catch e As Exception
            Console.WriteLine(e.ToString())
        End Try
    End Sub 'Main
    Private Shared Sub DetermineExists(ByVal fi As FileInfo, ByVal fileName As String)
        If fi.Exists Then
            ' Determine whether the file exists.
            Console.WriteLine("The file '{0}' exists in the specified directory.", fileName)
        Else
            Console.WriteLine("The file '{0}' does not exist in the specified directory.", fileName)
        End If
    End Sub 'DetermineExists
End Class 'ExistsTest

[C#] 
using System;
using System.IO;

public class ExistsTest 
{
    public static void Main() 
    {
        string neFile = "nonexistentfile";
        // Open an existing file, or create a new one.
        FileInfo fi = new FileInfo(neFile);
        DetermineExists(fi, neFile);
        neFile = "newFile.txt";
        // Create the file on disk.
        fi = new FileInfo(neFile);
        FileStream fs = fi.Create();
        DetermineExists(fi, neFile);
        // Close the file so that it can be deleted.
        fs.Close();
        // Delete the file.
        try 
        {
            fi.Delete();
            Console.WriteLine("The file '{0}' was deleted successfully.", fi.Name);
        } 
        catch (Exception e) 
        {
            Console.WriteLine(e.ToString());
        }
    }
    private static void DetermineExists( FileInfo fi, string fileName ) 
    {
        // Determine whether the file exists.
        if (fi.Exists)
            Console.WriteLine("The file '{0}' exists in the specified directory.", fileName);
        else
            Console.WriteLine("The file '{0}' does not exist in the specified directory.", fileName);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

void DetermineExists(FileInfo* fi, String* fileName) {
    // Determine whether the file exists.
    if (fi->Exists)
        Console::WriteLine(S"The file '{0}' exists in the specified directory.", fileName);
    else
        Console::WriteLine(S"The file '{0}' does not exist in the specified directory.", fileName);
}

int main() {
    String* neFile = S"nonexistentfile";
    // Open an existing file, or create a new one.
    FileInfo* fi = new FileInfo(neFile);
    DetermineExists(fi, neFile);
    neFile = S"newFile.txt";
    // Create the file on disk.* fi = new FileInfo(neFile);
    FileStream* fs = fi->Create();
    DetermineExists(fi, neFile);
    // Close the file so that it can be deleted.
    fs->Close();
    // Delete the file.
    try {
        fi->Delete();
        Console::WriteLine(S"The file '{0}' was deleted successfully.", fi->Name);
    } catch (Exception* e) {
        Console::WriteLine(e);
    }
}

[JScript] 
import System;
import System.IO;

public class ExistsTest {
    public static function Main() : void {
        var neFile : String = "nonexistentfile";
        // Open an existing file, or create a new one.
        var fi : FileInfo = new FileInfo(neFile);
        DetermineExists(fi, neFile);
        neFile = "newFile.txt";
        // Create the file on disk.
        fi = new FileInfo(neFile);
        var fs : FileStream = fi.Create();
        DetermineExists(fi, neFile);
        // Close the file so that it can be deleted.
        fs.Close();
        // delete the file
        try {
            fi.Delete();
            Console.WriteLine("The file '{0}' was deleted successfully.", fi.Name);
        } catch (e : Exception) {
            Console.WriteLine(e.ToString());
        }
    }
    private static function DetermineExists( fi : FileInfo, fileName : String ) : void {
        // figure out if the file exists or not
        if (fi.Exists)
            Console.WriteLine("The file '{0}' exists in the specified directory.", fileName);
        else
            Console.WriteLine("The file '{0}' does not exist in the specified directory.", fileName);
    }
}
ExistsTest.Main();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

FileInfo Class | FileInfo Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2017 Microsoft