C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: File.Exists will not throw an exception if the file does not exist. This makes it helpful to call before using other file methods.
So: Using File.Exists is a way to prevent exceptions from other types. StreamReader, for example, will throw if a file is not found.
StreamReaderVB.NET program that uses File.Exists
Imports System.IO
Module Module1
Sub Main()
' See if the file exists in the program directory.
If File.Exists("TextFile1.txt") Then
Console.WriteLine("The file exists.")
End If
' Check a file in the C volume.
Dim exists As Boolean = File.Exists("C:\lost.txt")
Console.WriteLine(exists)
End Sub
End Module
Output
The file exists.
False
Tip: In some situations, File.Exists is still useful. It may prevent an incorrect state in memory.