TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET File.Exists: Get Boolean

Use the File.Exists Function, which returns a Boolean true if the file is found on the disk.
File.Exists. Does a file exist? And does it matter whether it exists or not? We can answer the first question with File.Exists. The second question depends on the program context. File.Exists, part of System.IO, returns a Boolean.BooleanFile
Example. This program calls File.Exists twice. Please notice first how it imports the System.IO namespace. Around the first File.Exists call, it uses an if-statement. In the second call, it stores the result of File.Exists in a boolean value.

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.

StreamReader
VB.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
Discussion. File.Exists internally accesses the disk. This causes an IO read. Sometimes it is better to simply try to read a file, as with StreamReader. Then catch exceptions if the file is not found. This has fewer steps.
Further, exception handling is faster in many cases than disk reads. In the memory hierarchy, reading from a disk is often much slower than any CPU computation. Therefore, skipping File.Exists and just trying to read files may be better.Locality of Reference

Tip: In some situations, File.Exists is still useful. It may prevent an incorrect state in memory.

Summary. Existence is a complicated subject. File.Exists does not consider this though. It returns True, if a file exists. And it returns False if one does not. It is used within an If-statement conditional, and it may be stored in a Boolean field.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf