
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
726
|
Chapter 12: Filesystem I/O
wraps the IntPtr file handle and allows the system to handle the releasing of this file
handle automatically. To automatically release this wrapped file handle, you must
pass
true as the second argument to the SafeFileHandle constructor. Microsoft rec-
ommends letting the system handle the releasing of this wrapped file handle.
Keep your code short when opening a file using a file handle. Call the
FileStream.
Close
method as soon as possible or use the using statement as in the Solution for
this recipe. One reason for this recommendation is that another object might also
have this file open, and operating on that file through both
FileStream objects can
corrupt the data in the file.
See Also
See the “DllImport Attribute,” “File Class,” and “FileStream Class” topics in the
MSDN documentation.
12.20 Writing to Multiple Output Files at One Time
Problem
Any output that is written to one file must also be written to at least one other file.
Essentially, you want to end up with at least the original file and a duplicate file.
Solution
Create a class called MultiWriter with the ability to write to multiple files from a sin-
gle
WriteLine call.
To create a set of files, just pass the file paths you would like to use to the construc-
tor like this:
// Create a list of three ...