Reputation: 29
I believe this is a straight forward question, hope I wont get fumed. Writing a code to find the file named text.txt in C drive
If IO.File.Exists("C:\text.txt") Then
MessageBox.Show("Found text file")
Else
MessageBox.Show("Not Found")
End If
"C:\text.txt" cannot be found when located in a subfolder. What syntax should be used for this?
I have overspent my time on finding the solution, hence asking an easy question over here.
Thanks!
Upvotes: 0
Views: 4481
Reputation: 11105
You can use Directory.GetFiles (String, String, SearchOption) method to returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
For example:
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
Upvotes: 5
Reputation: 37
try this
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
or take a look at this link
How to check if a file exists in a folder?
Upvotes: 0