Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Check If File Exists with VB.NET

I was creating an application in Visual Basic the other day and I couldn’t remember how to check to see if a file exists, so I thought I would do a quick example to see if a file exitsts. If the file exists, the program will continue on, however, if the file doesn’t exist you will get a message box saying that the file does not exist, and the application will exit.

1
2
3
4
If System.IO.File.Exists("C:\MyDocs\MyFile.txt") = False Then
   MsgBox("Field does not exist!", MsgBoxStyle.Critical, "File Not Found")
   Application.Exit()
End If

If you have an Imports System.IO call in your program, then you can shorten the method call by deleting the System.IO class name, which would give you.

1
2
3
4
If File.Exists("C:\MyDocs\MyFile.txt") = False Then
   MsgBox("Field does not exist!", MsgBoxStyle.Critical, "File Not Found")
   Application.Exit()
End If

This method call is supported in .NET Framework version 3.5, 3.0, 2.0, 1.1, and 1.0.

3 comments to Check If File Exists with VB.NET

Leave a Reply