Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 7. Files, Directories, and Streams > The Stream Type - Pg. 267

Chapter 7 Files, Directories, and Streams 267 The FileSecurity type exposes other methods that enable you to modify the set of access rules contained in the object: ModifyAccessRule changes an existing access rule; PurgeAccessRules removes all the rules associated with a given IdentityReference object; RemoveAccessRule- Specific removes a specific access rule; ResetAccessRule adds the specified access rule and removes all the matching rules in one operation. Read MSDN documentation for details about these methods. The FileSecurity object is also capable of reading and modifying the audit rules associated with a file or directory. (Audit rules specify which file operations on a file, either successful or not, are logged by the system.) For example, the following code displays all the audit rules associated with a file: FileSecurity fsec = new FileSecurity("data.txt", AccessControlSections.All); Console.WriteLine("{0,-25}{1,-30}{2,-8}{3,-6}", "User", "Rights", "Outcome", "Inherited"); Console.WriteLine(new string('-', 72)); foreach ( FileSystemAuditRule fsar in fsec.GetAuditRules(true, true, typeof(NTAccount)) ) { Console.WriteLine("{0,-25}{1,-30}{2,-8}{3,-6}", fsar.IdentityReference.Value, fsar.FileSystemRights, fsar.AuditFlags, fsar.IsInherited); } The AuditFlags property is an enumerated value that can be Success or Failure. You can use the AddAuditRule method to add an audit rule to the FileSecurity object, the ModifyAudit- Rule method to change an existing audit rule, and so forth. The Stream Type The Stream abstract type represents a sequence of bytes going to or coming from a storage medium (such as a file) or a physical or virtual device (such as a parallel port, an interprocess communication pipe, or a TCP/IP socket). Streams enable you to read from and write to a backing store, which can correspond to one of several storage mediums. For example, you can have file streams, memory streams, and network streams. Because it's an abstract class, you don't create a Stream object directly, and you rarely use a Stream variable in your code. Rather, you typically work with types that inherit from it, such as the FileStream and the NetworkStream types. Stream Operations The fundamental operations you can perform on streams are read, write, and seek. Not all types of streams support all these operations--for example, the NetworkStream object doesn't support seeking. You can check which operations are allowed by using the stream's CanRead, CanWrite, and CanSeek properties. Most stream objects perform data buffering in a transparent way. For example, data isn't immediately written to disk when you write to a file stream; instead, bytes are buffered and are