validating a file using OFD

bizjosh

Member
Joined
Mar 16, 2005
Messages
23
Programming Experience
Beginner
when my prog tries to open a filedialog to open a .txt file, when
it fails to open it eg: when file is open or in used. It crashes,
anyway to turn around this. so that it can prompt an error msg
when the .txt file is not valid or not of a proper format instead
of crashing. also is it somehow possible to ensure the user opens
only .txt files else it will prompt, file not valid.

Private Sub btnFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileOpen.Click
ofdFile.Filter = "txt Files (*.txt)|*.txt|All Files (*.*)|*.*"
ofdFile.DefaultExt = "txt"
If ofdFile.ShowDialog() = DialogResult.Cancel Then
Return
End If
 
use a Try/Catch block around the code that opens/uses the file IE:
VB.NET:
Try
     'Open the file and read the data then close the file here
Catch ex As Exception
  'If there's an error code code to handle the error is put here
  Messagebox.Show(ex.Message)
End Try
 
Back
Top