MCTS Exam 70-536 – Reading and Writing Streams and Files
Dienstag, 06. Oktober 2009Good Morning folks,
Today I read about streams and files, and as usual I provide my notes as a summary. enjoy !
Textfiles
- To read a textfile, we can use TextReader or StreamReader Class
- To write a textfile, we can use TextWriter or StreamWriter Class
- StreamReader derives from TextReader
- Usually you use File.OpenText or StreamReader Constructor
- Typically we use ReadLine or ReadToEnd Methods to read data
- Dont forget to call Close on the Reader or Writer after finished reading or writing
- To ensure, that no data is left in the buffer while keeping the file open use the Flush Method
Binary Files
- To read and write binary files, use BinaryReader and BinaryWriter
- Generally serialization is more effective
Strings
- Use StringWriter to write data to StringBuilder
- Not used very often, only in special scenarios
MemoryStream
- To create a stream in memory
- Commonly used to store data temporarily that will be written to a file eventually
- To Minimize the time a file is locked open, minimizes the potential for conflict
- MemoryStream often is used with a StreamWriter, cause MS itself has only WriteByte/Write, and ReadByte/Read Methods that work with bytes and byte-arrays
BufferedStream
- Is used for custom stream implements
- .NET Stream Classes have a build-in buffering logic, so you normally dont need to use BufferedStream. If you do so its redundant and inefficent
Compressed Streams
- You can only read or write bytes and byte-arrays directly, but you can use StreamWriter and StreamReader
- Use GZipStream Class for GZIP-Compression and DeflatedStream class for Deflated Data Format
- CompressionMode which is part of the constructor indicates simply if you are compressing or decompressing data
Isolated Storage
- Isolated Storage is a private file system
- It requires fewer privileges than writing to filesystem
- IS is isolated by user, application domain and assembly
- Don’t use to store high-value secrets or sensitive data, because its not protected from high-trusted code, unmanaged code or trusted users of the computer
- The access to a file is restricted to the user who created it
- isolation by application domain is optional
- In most case (almost always) you will use the isolation by application-domain
- Classes to work with Isolated Storage (All in System.IO.IsolatedStorage)
- IsolatedStorageFile – Management of Isolated Storage Stores (Often Used Methods are .GetUserStoreForAssembly, .GetUserStoreForDomain, .GetStore
- IsolatedStorageFileStream – Access to read/write Isolated Storage files
- IsolatedStorageException – exception related to Isolated Sotrage
- Code must be granted IsolatedStorageFilePermission

