Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
File Class
File Methods
 Exists Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
File..::.Exists Method

Determines whether the specified file exists.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function Exists ( _
    path As String _
) As Boolean
Visual Basic (Usage)
Dim path As String
Dim returnValue As Boolean

returnValue = File.Exists(path)
C#
public static bool Exists(
    string path
)
Visual C++
public:
static bool Exists(
    String^ path
)
JScript
public static function Exists(
    path : String
) : boolean

Parameters

path
Type: System..::.String
The file to check.

Return Value

Type: System..::.Boolean
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 nullNothingnullptra 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.

Visual Basic
Imports System
Imports System.IO


Public Class Test
    Public Shared Sub Main()
        Dim sourceFile As String = "c:\tests\Test.txt"
        Dim newFile As String = "c:\tests\Test2.txt"

        If File.Exists(sourceFile) Then
            File.Copy(sourceFile, newFile)
        Else
            Console.WriteLine("Source file does not exist.")
        End If
    End Sub
End Class
C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string sourceFile = @"c:\tests\Test.txt";
        string newFile = @"c:\tests\Test2.txt";
        if (File.Exists(sourceFile))
        {

            File.Copy(sourceFile, newFile);

        }
        else
        {
            Console.WriteLine("Source file does not exist.");

        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
What try...catch block?      Craigfis ... Thomas Lee   |   Edit   |   Show History
The text says that "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" but the example does not use a try...catch block.
Sample using PowerShell      Thomas Lee   |   Edit   |   Show History
  

<#
.SYNOPSIS
This script displays the usage of the Exists and the
copy methods of System.IP.File
.DESCRIPTION
This script sets up two file names, then checks to see if
the sorucefile exists. if so, it's copied to a new file.
.NOTES
File Name : copy-file.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2 CTP3
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
#>

##
# start of script
##

# Setup source and destination files
$SourceFile = "c:\foo\Test.txt";
$NewFile = "c:\foo\Test2.txt";

# Now - check to see if $Sourcefile exists, and if so,
# copy it to $newfile
if ([System.IO.File]::Exists($SourceFile)) {
[System.IO.File]::Copy($SourceFile, $NewFile)
"Source File ($SourceFile) copied to ($newFile)"
}
else {
"Source file ($Sourcefile) does not exist."
}
# End of script

wrong example description      joshzilla   |   Edit   |   Show History
The description to an example reads "The following example uses the Exists method to help ensure that a file is not overwritten."
But all the example does is make sure the source file exists, happily overwriting any existing files.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker