I have been trouble with an cast error for the following method in a PCL which I have no issues with in a legacy class library.
protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
{
FolderInfo folderInfo = new FolderInfo(folderPath);
if (settings.MaxDepthLevel == 0 || level < settings.MaxDepthLevel)
{
try
{
DirectoryInfo currentDirectoryInfo = new DirectoryInfo(folderPath);
foreach (DirectoryInfo directoryInfo in currentDirectoryInfo.GetDirectories())
{
if (settings.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
FolderInfo subFolderInfo = GetFolderInfo(directoryInfo.FullName, level + 1);
folderInfo.Folders.Add(subFolderInfo);
subFolderInfo.Parent = folderInfo;
}
foreach (FileInfo fileInfo in currentDirectoryInfo.EnumerateFiles())
{
if (settings.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
folderInfo.Files.Add(fileInfo);
}
folderInfo.Files.Sort((x, y) => x.Name.CompareTo(y.Name));
}
catch (UnauthorizedAccessException)
{
}
}
return folderInfo;
}
Exception thrown: 'System.InvalidCastException' in System.IO.FileSystem.dll Additional information: Unable to cast object of type 'System.IO.FileSystemInfo[]' to type 'System.Collections.Generic.IEnumerable`1[System.IO.DirectoryInfo]'.
In a PCL, how differently could I write this above 'working' code to behave differently?
Thanks and best regards Michael