Export (0) Print
Expand All
4 out of 12 rated this helpful - Rate this topic

File.Exists Method

Determines whether the specified file exists.

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

public static bool Exists (
	string path
)
public static boolean Exists (
	String path
)
public static function Exists (
	path : String
) : boolean

Parameters

path

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.

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";
        try 
        {
            using (StreamWriter sw = File.CreateText(path)) {}

            // 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 
        {
            Console.WriteLine("Double copying is not allowed, as expected.");
        }
    }
}

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 {
                sw.Dispose();
            }

            // 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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.