I want to check if each item in a directory is of type file or a folder in using C#. I want to write something like the code below. But IsInstanceOfType(FileInfo)and IsInstanceOfType(DirectoryInfo) is not allowed in C#.
DirectoryInfo dirPrograms = new DirectoryInfo(folderPath);
foreach(var dir in dirPrograms.EnumerateFileSystemInfos())
{
if (dir.GetType().IsInstanceOfType(FileInfo))
//do something
elseif (dir.GetType().IsInstanceOfType(DirectoryInfo))
//do something
}
You can use the
isoperator to check whether an object can be converted to a specific type: