Programming folder those will get created

Info icon This preview shows pages 412–414. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming CSharp\FindDuplicates folder— those will get created automatically the first time we run as a result of creating the temporary folders inside them.) A side effect of this is that it is safe to call it if all of the directories in the path already exist—it will just do nothing. In addition to the overload we’ve used, there’s a second which also takes a Directory Security parameter: Directory.CreateDirectory(string path, DirectorySecurity directorySecurity) The DirectorySecurity class allows you to specify filesystem access controls with a relatively simple programming model. If you’ve tried using the Win32 ACL APIs, you’ll know that it is a nightmare of GUIDs, SSIDs, and lists sensitive to item ordering. This model does away with much of the complexity. Let’s extend our create function to make sure that only our current user has read/write/ modify permissions on these directories. Example 11-15 modifies the previous example by explicitly granting the current user full control of the newly created folders. The new or changed lines are highlighted. Example 11-15. Configuring access control on new directories private static string[] MakeTestDirectories() { string localApplicationData = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData), @"Programming CSharp\FindDuplicates"); // Get the name of the logged in user string userName = WindowsIdentity.GetCurrent().Name; // Make the access control rule FileSystemAccessRule fsarAllow = new FileSystemAccessRule( userName, FileSystemRights.FullControl, AccessControlType.Allow); DirectorySecurity ds = new DirectorySecurity(); ds.AddAccessRule(fsarAllow); // Let's make three test directories var directories = new string[3]; for (int i = 0; i < directories.Length; ++i) { string directory = Path.GetRandomFileName(); // Combine the local application data with the // new random file/directory name 388 | Chapter 11: Files and Streams
Image of page 412

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

View Full Document Right Arrow Icon
string fullPath = Path.Combine(localApplicationData, directory); // And create the directory Directory.CreateDirectory(fullPath, ds); directories[i] = fullPath; Console.WriteLine(fullPath); } return directories; } You’ll need to add a couple of using directives to the top of the file before you can compile this code: using System.Security.AccessControl; using System.Security.Principal; What do these changes do? First, we make use of a type called WindowsIdentity to find the current user, and fish out its name. If you happen to want to specify the name explicitly, rather than get the current user programmatically, you can do so (e.g., MYDOMAIN\SomeUserId ). Then, we create a FileSystemAccessRule , passing it the username, the FileSystem Rights we want to set, and a value from the AccessControlType enumeration which determines whether we are allowing or denying those rights.
Image of page 413
Image of page 414
This is the end of the preview. Sign up to access the rest of the document.

{[ 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