vb.net 如何检测文件是否未找到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13780002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:29:46  来源:igfitidea点击:

How to detect if file isn't found

vb.net

提问by user1877499

I'm a bit new to VB.NET. I was wondering if there was a way to detect if a file being open doesn't exist, then something will happen. Is this possible and is it possible using the "If" statement?

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

回答by xpda

You can use file.exists(filename)to check before you open it, or a try-catch block:

您可以file.exists(filename)在打开它之前使用to check 或 try-catch 块:

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

or

或者

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

You can add imports system.ioat the top of your file to use File.Existsinstead of System.IO.File.Exists.

您可以imports system.io在文件顶部添加以File.Exists代替System.IO.File.Exists.