If we modify the linq query to use this as shown in

Info icon This preview shows pages 431–434. Sign up to view the full content.

If we modify the LINQ query to use this, as shown in Example 11-30 , the overall pro- gress will be undisturbed by the error handling. Example 11-30. Iterating in the face of errors var allFilePaths = from directory in directoriesToSearch from file in GetDirectoryFiles(directory, searchOption) select file; And we can use a similar technique for the LINQ query that populates the fileNameGroups —it uses FileInfo , and we need to handle exceptions for that. Exam- ple 11-31 iterates through a list of paths, and returns details for each file that it was able to access successfully, displaying errors otherwise. Example 11-31. Handling exceptions from FileInfo private static IEnumerable<FileDetails> GetDetails(IEnumerable<string> paths) { foreach (string filePath in paths) { FileDetails details = null; try { FileInfo info = new FileInfo(filePath); details = new FileDetails { FilePath = filePath, FileSize = info.Length }; When Files Go Bad: Dealing with Exceptions | 407
Image of page 431

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

} catch (FileNotFoundException fnfx) { Console.WriteLine("Warning: The specified file was not found"); Console.WriteLine(fnfx.Message); } catch (IOException iox) { Console.Write("Warning: "); Console.WriteLine(iox.Message); } catch (UnauthorizedAccessException uax) { Console.WriteLine( "Warning: You do not have permission to access this file."); Console.WriteLine(uax.Message); } if (details != null) { yield return details; } } } We can use this from the final LINQ query in InspectDirectories . Example 11-32 shows the modified query. Example 11-32. Getting details while tolerating errors var fileNameGroups = from filePath in allFilePaths let fileNameWithoutPath = Path.GetFileName(filePath) group filePath by fileNameWithoutPath into nameGroup select new FileNameGroup { FileNameWithoutPath = nameGroup.Key, FilesWithThisName = GetDetails(nameGroup).ToList() }; Again, this enables the query to process all accessible items, while reporting errors for any problematic files without having to stop completely. If we compile and run again, we see the following output: C:\Users\mwa\AppData\Local\dcyx0fv1.hv3 C:\Users\mwa\AppData\Local\0nf2wqwr.y3s C:\Users\mwa\AppData\Local\kfilxte4.exy Warning: You do not have permission to access this directory. Access to the path 'C:\Users\mwa\AppData\Local\r2gl4q1a.ycp\' is denied. SameNameAndContent.txt ---------------------- C:\Users\mwa\AppData\Local\dcyx0fv1.hv3 C:\Users\mwa\AppData\Local\0nf2wqwr.y3s C:\Users\mwa\AppData\Local\kfilxte4.exy 408 | Chapter 11: Files and Streams
Image of page 432
We’ve dealt cleanly with the directory to which we did not have access, and have con- tinued with the job to a successful conclusion. Now that we’ve found a few candidate files that may (or may not) be the same, can we actually check to see that they are, in fact, identical, rather than just coincidentally having the same name and length? Reading Files into Memory To compare the candidate files, we could load them into memory. The File class offers three likely looking static methods: ReadAllBytes , which treats the file as binary, and loads it into a byte array; File.ReadAllText , which treats it as text, and reads it all into a string; and File.ReadLines
Image of page 433

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 434
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern