A first attempt at handling io exceptions try

Info icon This preview shows pages 426–429. Sign up to view the full content.

Example 11-24. A first attempt at handling I/O exceptions try { List<FileNameGroup> filesGroupedByName = InspectDirectories(recurseIntoSubdirectories, directoriesToSearch); DisplayMatches(foundFiles); Console.ReadKey(); } 402 | Chapter 11: Files and Streams
Image of page 426

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

catch (PathTooLongException ptlx) { Console.WriteLine("The specified path was too long"); Console.WriteLine(ptlx.Message); } catch (DirectoryNotFoundException dnfx) { Console.WriteLine("The specified directory was not found"); Console.WriteLine(dnfx.Message); } catch (IOException iox) { Console.WriteLine(iox.Message); } catch (UnauthorizedAccessException uax) { Console.WriteLine("You do not have permission to access this directory."); Console.WriteLine(uax.Message); } catch (ArgumentException ax) { Console.WriteLine("The path provided was not valid."); Console.WriteLine(ax.Message); } finally { if (testDirectoriesMade) { CleanupTestDirectories(directoriesToSearch); } } We’ve decided to provide specialized handling for the PathTooLongException and DirectoryNotFoundException exceptions, as well as generic handling for IOException (which, of course, we have to catch after the exceptions derived from it). In addition to those IOException -derived types, we’ve also caught UnauthorizedAcces sException . This is a security exception, rather than an I/O exception, and so it derives from a different base ( SystemException ). It is thrown if the user does not have permission to access the directory concerned. Let’s see that in operation, by creating an additional test directory and denying our- selves access to it. Example 11-25 shows a function to create a directory where we deny ourselves the ListDirectory permission. Example 11-25. Denying permission private static string CreateDeniedDirectory(string parentPath) { string deniedDirectory = Path.GetRandomFileName(); string fullDeniedPath = Path.Combine(parentPath, deniedDirectory); string userName = WindowsIdentity.GetCurrent().Name; DirectorySecurity ds = new DirectorySecurity(); FileSystemAccessRule fsarDeny = When Files Go Bad: Dealing with Exceptions | 403
Image of page 427
new FileSystemAccessRule( userName, FileSystemRights.ListDirectory, AccessControlType.Deny); ds.AddAccessRule(fsarDeny); Directory.CreateDirectory(fullDeniedPath, ds); return fullDeniedPath; } We can call it from our MakeTestDirectories method, as Example 11-26 shows (along with suitable modifications to the code to accommodate the extra directory). Example 11-26. Modifying MakeTestDirectories for permissions test private static string[] MakeTestDirectories() { // ... // Let's make three test directories // and leave space for a fourth to test access denied behavior var directories = new string[ 4 ]; for (int i = 0; i < directories.Length - 1 ; ++i) { ... as before ... } CreateTestFiles(directories .Take(3) ); directories[3] = CreateDeniedDirectory(localApplicationData); return directories; } But hold on a moment, before you build and run this. If we’ve denied ourselves per- mission to look at that directory, how are we going to delete it again in our cleanup code? Fortunately, because we own the directory that we created, we can modify the permissions again when we clean up.
Image of page 428

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

Image of page 429
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