2

我对 VB.NET 有点陌生。我想知道是否有办法检测正在打开的文件是否不存在,然后会发生一些事情。这可能吗?是否可以使用“If”语句?

4

1 回答 1

6

您可以file.exists(filename)在打开它之前使用检查,或者使用 try-catch 块:

If not System.IO.File.Exists(filename) Then
  ' file does not exist
  end if

或者

Try
  open ...
Catch ex As Exception
  MsgBox(ex.Message)  ' not-found error handling goes here
End Try

您可以imports system.io在文件顶部添加使用File.Exists而不是System.IO.File.Exists.

于 2012-12-08T20:10:10.093 回答