我在使用VB.NET,当表单加载时,它会检查文件是否存在。如果找到该文件,则继续加载,否则会出现错误框并退出。
像
这样的东西Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim file As FileStream = New FileStream("target.txt", FileMode.Open, FileAccess.Read)
Catch ex As System.IO.FileNotFoundException
'code to stop form loading goes here.
End Sub
End Class
答案 0 :(得分:1)
你的意思是这样吗?:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim file As FileStream = New FileStream("target.txt", FileMode.Open, FileAccess.Read)
'the rest of your "load" code goes here...
Catch ex As System.IO.FileNotFoundException
MessageBox.Show("File not found", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Close()
End Try
End Sub
如果找不到文件,它将显示MessageBox
并关闭表单。