Tim*_*Tim 0 .net vb.net directory file find
我相信这是一个直截了当的问题,希望我不会感到生气。编写代码以在C驱动器中找到名为text.txt的文件
If IO.File.Exists("C:\text.txt") Then
MessageBox.Show("Found text file")
Else
MessageBox.Show("Not Found")
End If
Run Code Online (Sandbox Code Playgroud)
当位于子文件夹中时,找不到“ C:\ text.txt”。为此应使用什么语法?
我在寻找解决方案上花了很多时间,因此在这里提出了一个简单的问题。
谢谢!
您可以使用Directory.GetFiles(String,?String,?SearchOption)方法来返回与指定目录中的指定搜索模式匹配的文件名(包括其路径),并使用一个值来确定是否搜索子目录。
例如:
If System.IO.Directory.GetFiles("C:\Users\You\Desktop", "file.txt", IO.SearchOption.AllDirectories).Length > 0 Then
MsgBox("Found!")
Else
MsgBox("Not found!")
End If
Run Code Online (Sandbox Code Playgroud)