File.Exists Method
Determines whether the specified file exists.
[Visual Basic] Public Shared Function Exists( _ ByVal path As String _ ) As Boolean [C#] public static bool Exists( string path ); [C++] public: static bool Exists( String* path ); [JScript] 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) 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.
Remarks
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.
For an example of using this method, 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 |
| Copy a file. | File.Copy |
| Rename or move a file. | File.Move |
| 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
[Visual Basic, C#, C++] The following example uses the Exists method to ensure that a file is not overwritten.
[Visual Basic] 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" Try Dim sw As StreamWriter = File.CreateText(path) sw.Close() ' 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.") Else 'try to copy it File.Copy(path, path2) Console.WriteLine("{0} was copied to {1}.", path, path2) End If Else 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 [C#] 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."); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::IO; int main() { String* path = S"c:\\temp\\MyTest.txt"; String* path2 = String::Concat(path, S"temp"); try { StreamWriter* sw = File::CreateText(path); if (sw) __try_cast<IDisposable*>(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(S"The target already exists"); } else { // Try to copy the file. File::Copy(path, path2); Console::WriteLine(S"{0} was copied to {1}.", path, path2); } } else { Console::WriteLine(S"The source file does not exist."); } } catch (Exception*) { Console::WriteLine(S"Double copying is not allowed, as expected."); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
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, Common Language Infrastructure (CLI) Standard
.NET Framework Security:
- FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read
See Also
File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File