1

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;
    }

I get the following error: 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]'.

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

McoreD
  • 313
  • 2
  • 12
  • Bigger question is why System.IO.FileSystem.dll is being used. You do have to document the target you tested. It definitely looks broken in CoreFx right now, a rather plain casting bug. File a bug with the project. – Hans Passant Feb 10 '16 at 14:56
  • Thanks I submitted a problem through VS2015. – McoreD Feb 10 '16 at 22:19

1 Answers1

0

Windows Phone applications do not use the file system of the operating system and are restricted to using isolated storage to persist and access files, so this namespace does not provide any additional functionality.

http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.io%28v=vs.105%29.aspx

You should also take a look at this SO answer.

Peroxy
  • 646
  • 8
  • 18
  • Thanks - unfortunately I cannot use PCLStorage either: PCLStorage 1.0.2 is not compatible with DNXCore,Version=v5.0 (win7-x86). Microsoft.Bcl.Async 1.0.165 is not compatible with DNXCore,Version=v5.0 (win7-x86). Microsoft.Bcl 1.1.6 is not compatible with DNXCore,Version=v5.0 (win7-x86). Some packages are not compatible with DNXCore,Version=v5.0 (win7-x86). – McoreD Feb 10 '16 at 22:19