15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

802 ❘ ChaPTer 29 mAnipulAtinG files And the reGistry<br />

figure 29-15<br />

From here, you can see that these message boxes provide details about three entirely different drives. The<br />

first, drive C:\, is my hard drive, as the message box shows its drive type as Fixed. The second drive, drive<br />

D:\, is my CD/DVD drive. The third drive, drive F:\, is my USB pen <strong>and</strong> is labeled with a Removable<br />

drive type.<br />

file seCuriTy<br />

When the .<strong>NET</strong> Framework 1.0/1.1 was first introduced, it didn’t come with a way to easily access <strong>and</strong> work<br />

access control lists (ACLs) for files, directories, <strong>and</strong> registry keys. To do such things at that time usually<br />

meant some work with COM interop, thus also requiring a more advanced programming knowledge of<br />

working with ACLs.<br />

This has considerably changed after the release of the .<strong>NET</strong> Framework 2.0 came out. This made the<br />

process of working with ACLs considerably easier with a namespace — System.Security.AccessControl.<br />

With this namespace, it is now possible to manipulate security settings for files, registry keys, network<br />

shares, Active Directory objects, <strong>and</strong> more.<br />

reading aCls from a file<br />

For an example of working with System.Security.AccessControl, this section looks at working with the<br />

ACLs for both files <strong>and</strong> directories. It starts by looking at how you would review the ACLs for a particular<br />

file. This example is accomplished in a console application <strong>and</strong> is illustrated here:<br />

using System;<br />

using System.IO;<br />

using System.Security.AccessControl;<br />

using System.Security.Principal;<br />

namespace ReadingACLs<br />

{<br />

internal class Program<br />

{<br />

private static string myFilePath;<br />

private static void Main()<br />

{<br />

Console.Write("Provide full file path: ");<br />

myFilePath = Console.ReadLine();<br />

try<br />

{<br />

using (FileStream myFile =<br />

new FileStream(myFilePath, FileMode.Open, FileAccess.Read))<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!