0

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!

Tim
  • 29
  • 1
  • 7
  • If you've read the documentation for `File.Exists` then you already know the answer. If you haven't already read the documentation, why haven't you read the documentation? The Help menu is not in VS for decoration. If you want help on a type or method then use the Help menu to open the documentation for that type or method. Only after doing that should you consider other sources of information if you need them. – jmcilhinney Sep 29 '17 at 07:09
  • This is C# but https://stackoverflow.com/questions/3994448/how-to-check-if-a-specific-file-exists-in-directory-or-any-of-its-subdirectories – lc. Sep 29 '17 at 07:12
  • @jmcilhinney Thank you for reply, I have read File.Exists doc and it seems that to find the file, it has to be pointing to the exact location(subfolder). I'm still a newbie in programming therefore still pretty unsure the best way to find the solution. – Tim Sep 29 '17 at 07:14
  • @lc. Hi thanks, I came across that as well, not really understand though. – Tim Sep 29 '17 at 07:17
  • My sincere apologies. I was completely thinking of `Directory.GetFiles` when I made that comment. D'oh! – jmcilhinney Sep 29 '17 at 08:02

2 Answers2

5

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
tezzo
  • 10,858
  • 1
  • 25
  • 48
  • Hi, I implemented ur suggestion Directory.GetFiles(@"C:\", "text.txt", SearchOption.AllDirectories) it gave an error on "@" expression expected. Any thought on this? Thanks! – Tim Sep 29 '17 at 08:12
  • @Tim : Remove the '@', it is only applicable in C# code. – Visual Vincent Sep 29 '17 at 08:17
  • @VisualVincent Hi, it appears that the get directory cant get in if statement...."1 dimensional array of string cannot be converted to boolean"... Please enlighten me. Thanks. – Tim Sep 29 '17 at 08:36
  • this method returns an array of string containing every result of your search (i.e. C:\Folder\test.txt; C:\Folder2\a\test.txt). Added example to my answer. – tezzo Sep 29 '17 at 09:13
  • @tezzo Thanks! Apparently I missed out the .Length > 0. Does the > 0 means path exist? – Tim Sep 30 '17 at 02:20
  • 1
    @Tim : `.Length` returns _how many_ files it has found matching the criteria (files with the name `test.txt`). `.Length > 0` checks if **at least** one file has been found. – Visual Vincent Sep 30 '17 at 07:45
0

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?

RAS RASS
  • 37
  • 7