Wednesday, November 2, 2022

[FIXED] How to check for any files with FileExists(String) method

Issue

I'm trying to automate a document handling process, and I need to check if there are any files inside a certain folder. The process itself removes the files from the folder once it finishes, so I need it to loop back and check if there are any files left.

So far I've been using a sample file like this:

File.Exists("C:\Users\gcaor\Desktop\OC\150.pdf")

150.pdf is the sample file it's searching for, but is there a way to search for any file at all? So that it returns true if there is a file in the folder and false if there isn't


Solution

You can use Directory.EnumerateFiles + Any:

Dim anyFileExist = Directory.EnumerateFiles(path).Any()

This is using standard .NET methods and also stops at the first file found.



Answered By - Tim Schmelter
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.