
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Choosing a Method of Opening a File or Stream for Reading and/or Writing
|
681
Discussion
Determining whether a file exists is often critical to your code. If a file exists and you
try to create it using one of the file-creation methods, one of three things will hap-
pen: the existing file will be overwritten, an exception will be thrown if the file is
read-only, or an exception will be thrown indicating that the state of the filesystem is
not what you think it is. There is a small window between the
Exists call and the
actions you take where another process could change the filesystem, so you should
be prepared for that with proper exception handling.
See Also
See the “File Class” topic in the MSDN documentation.
12.5 Choosing a Method of Opening a File or Stream
for Reading and/or Writing
Problem
When you are first learning the .NET Framework—and even for some time after—
the proper way to read to, write from, or otherwise interact with files can be unclear
because the framework provides so many different ways of attacking this problem.
How should you determine which approach fits your scenario?
Solution
Use file streams to perform various file functions. There are five basic types of built-
in file-stream manipulation classes that you can use to read from and/or write to a
file stream:
FileStream ...