Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on May 1, 2009 in Blog, Programming | 2 comments

VB.net check for existence of files and or folders


Add this to check the existence of a particular file:
Dim sFileName As String
sFileName = “C:/text1.txt”
Dim fFile As New FileInfo(sFileName)
If Not fFile.Exists Then
MessageBox.Show(“File Not Found”)
Else
MessageBox.Show(“File Found. File was created on: ” & fFile.CreationTime)
End If

Add this to check the existence of a directory:
Dim sDirName As String
sDirName = “C:/temp”
Dim dDir As New DirectoryInfo(sDirName)
If Not dDir.Exists Then
MessageBox.Show(“Directory Not Found”)
Else
MessageBox.Show(“Directory Found. Directory was last accessed on: ” & dDir.LastAccessTime)
End If

2 Comments

  1. Actually you don’t have to instantiate a new FileInfo object; you can just use System.IO.File.Exists(“path to file here”) as well as System.IO.Directory.Exists(“path here”)

    FileInfo is useful to instantiate if you need other info, however.

  2. That’s even better! Thanks for the info!

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>