How to check if a folder contains files in C# and VB.NET

To check if a folder contains files in C# and VB.NET you can use the following snippet below.

Sample C#

public static Boolean HasFilesInFolder(DirectoryInfo directory, string paramFilter)
{
	try
	{
		return directory.GetFiles(paramFilter).Any();
	}
	catch (Exception ex)
	{
		//handle the exception your way
		return false;
	}
}

Sample VB.NET

Public Shared Function HasFilesInFolder(directory As DirectoryInfo, paramFilter As String) As [Boolean]
	Try
		Return directory.GetFiles(paramFilter).Any()
	Catch ex As Exception
		'handle the exception your way
		Return False
	End Try
End Function

Please Share us:
Please rate this snippet

One thought on “How to check if a folder contains files in C# and VB.NET

Leave a Reply