DQbJA%{upLuyqLuPRyu{PydDQbJA%{_Vj9|yqLaFaDQDQbJA%Pyu'DQbJA%{upp9pUpPyu@Pp{m^{DQbJA%{gp99i]yu^DQDQbJA%{p9j9jypux]DQbJA%{aPUjPPSyqPaPSDQbJA%a(?J?B?{mxyuKaABbBEBXZCB~B| G57464Z G578ZC 74Z 94566Z 94Z 98ZB- G574F- 78ZZB 94ZZYBZBZZBZBZYYYZYY~BZBZBZBZ-~~-

File.` A message is carved into rock. This ancient city is full of life and beauty. In one moment the city is destroyed. But that message remains.`Consider a computer.` Data stored in a file lasts longer than that on memory. A file is persistent. In C# we handle files with System.IO.`StreamReader.` For text files, StreamReader and StreamWriter are often the most useful types. We use StreamReader in a "using" block (a special syntax form). `StreamReader `streamreader`Syntax forms like the using-statement are useful. They allow automatic cleanup of resources.`Using `using`ReadLine: `This is a method on StreamReader. It returns null if no further data is available in the file.`StreamReader: ReadLine `readline`StreamWriter.` This class writes strings or append strings to a text file. We can write numbers or the textual representation of anything. It also uses a "using" block. `StreamWriter `streamwriter`ReadAllText.` This program uses this method to load in the file "file.txt" on the C: volume. Then it prints the contents of the file. The data is now stored in a string object. `File.ReadAllText `file-readalltext`ReadAllText is the easiest way to put a file into a string. It is part of the System.IO namespace.`ReadAllLines.` Here we read all the lines from a file and place them in an array. The code reads lines from "file.txt" and uses a foreach-loop on them. This is efficient code. `File.ReadAllLines `file-readalllines`Count lines.` We count the number of lines in a file with few lines of code. The example here is a bit slow. But it works. It references the Length property. `Line Count `line-count`Query.` Does a line containing a specific string exist in the file? Maybe we want to see if a name or location exists in a line in the file. We use LINQ to find any matching line. `Count `count`ReadLines.` This method does not immediately read in every line. It instead reads lines only as they are needed. We use it in a foreach-loop. `File.ReadLines `file-readlines`Foreach `foreach`WriteAllLines.` We can write an array to a file. When we are done within-memory processing, we often need to write the data to disk. `WriteAllText.` A simple method, File.WriteAllText receives two arguments. It receives the path of the output file, and the exact string contents of the text file. `AppendAllText.` We could read in a file, append to that in memory, and then write it out completely again. That is slow. Its more efficient to use an append. `File.AppendAllText `file-appendalltext`ReadAllBytes.` We use File.ReadAllBytes to read an image (a PNG) into memory. With this code, we could cache an image in memory. It outperforms reading the image in each time. `File.ReadAllBytes `file-readallbytes`File.WriteAllBytes: Compress `compress`Path.` Before any file can be opened, it must be addressed. File paths are complex. They include the volume, directory, name and extension. `Path `path`Directory.` We can manipulate directories on the file system with System.IO. The Directory type, and its static methods, is necessary for this. `Directory `directory`Directory: Create `directory-createdirectory`Directory: Get Files `directory-getfiles`Directory: Size `directory-size`FileInfo.` We can get information about a file from the file system with FileInfo. This does not load the entire file into memory. It just reads stored stats. `FileInfo `fileinfo`FileInfo: Length `fileinfo-length`HTML, XML.` Some files have lots of brackets and tags. These are usually HTML or XML files. We could write custom methods for each program, but standardized approaches exist. `HTML, XML `html`TextReader.` The TextReader and TextWriter types form the base class that other, more useful types derive from. Usually they are not useful on their own. `TextReader `textreader`TextWriter `textwriter`Binary.` BinaryReader and BinaryWriter make reading or writing a binary file much easier. These types introduce a level of abstraction over the raw data. `BinaryReader `binaryreader`BinaryWriter `binarywriter`Actions.` We copy, delete, rename or get time information about files. These actions are available through the File type and the FileInfo type. `File.Copy `file-copy`File.Delete `file-delete`File.Exists `file-exists`File.GetLastWriteTimeUtc `file-getlastwritetimeutc`File.Move `file-move`File.Open `file-open`File.Replace `file-replace`Stream.` There are many forms of streams. Sometimes leaving a file on the disk would impact performance or stability in a negative way. In these cases, consider MemoryStream. `Stream `stream`FileStream `filestream`MemoryStream `memorystream`BaseStream `basestream`BufferedStream `bufferedstream`Seek: `We can seek to a specific location in a file with the Seek method. Seek is useful with large binary files.`Seek `seek`WebClient.` Not every file we want to use is local. A file may be remote. We may need to access the network to download a file from a server. `WebClient `webclient`HttpClient `httpclient`Office.` It is common to need to control Microsoft Excel with C# code. We introduce a fast approach. This material may be outdated, but it still helps on many systems. `Excel `excel`Word `word`CSV files.` These are text-based databases. With the System.IO namespace, we can read them into a C# program. Sadly the TextFieldParser is slow. `TextFieldParser: Parse CSV `textfieldparser`CSV: Separate Files `csv`Equality.` How can we tell if two files are exactly equal? Unfortunately, the file system's metadata is not sufficient. A method that compares each byte is effective. `File Equals `file-equals`Research.` The performance of file handling is an important part of computer programming. Often, optimizing how files are used is the most effective way to make a program faster. `Quote: `One of the most significant sources of inefficiency is unnecessary input/output (I/O) (Code Complete).`Quote: `We can build small and fast storage, or large and slow storage, but not storage that is both large and fast (Compilers: Principles, Techniques and Tools).`A final reminder.` Even with the helpful types provided in the .NET Framework, file handling is fraught with errors. We must account for disk errors and invalid data.

123 22.IO222 { 121{3 1122 every line2the file. 113using3 (322er3 reader2222er(3"file.txt"3)) 11{ 1112 line; 1112 ((line2reader.2Line()) != null) 111{3 11112Do something with the line. 111132[] parts2line.2(3','3); 111} 11} 1} }3 22.IO222 { 121{3 112C2 or open file2write line2it. 112If file exists, it contents are erased be2e writing. 113using3 (var writer22322r3(3@"C:\programs\e2.txt"3)) 11{ 111writer.2Line(3"HELLO"3); 11} 1} }3 22; 22.IO222 { 121{ 112 file23File.2AllText3(3"C:\\file.txt"3); 112file); 1} }3 22.IO222 { 121{3 11222every line2specified file. 112This will store all lines2an 22memory. 1132[] lines23File.2AllLines3(3"file.txt"3); 11323 (2 line2lines) 11{3 1112Do something with the line. 11132line.L2 > 80) 111{3 11112Important code. 1113} 11} 1} }3 22.IO222 { 121{3 112Another m2 of counting lines2a file. 112This is not the most efficient way. 112It counts empty lines. 1132line223File.2AllLines3(3"file.txt"3).L2; 1} }3 22.IO; 22.Linq222 { 121{3 112See if line exists2a file. 112Use a query expression2count matching lines. 112If one matches, the bool is set2true. 113bool exists2(from line23File.2AllLines3(3"file.txt"3) 11111 where line23"Some line match"3 11111 select line).2() > 0; 1} }3 22.IO222 { 121{3 1122 a 2 22a file. 1132[]3 22222[] 11{ 111"cat", 111"dog", 111"arrow" 11}; 113File.2AllLines3(3"file.txt"3, 22); 1} } 3Results: file.txt3 cat dog arrow3 22.IO222 { 121{ 113File.2AllText3(3"C:\\perls.txt"3, 1113"2"3); 1} }3 22ImageCache { 12byte[] _logoBytes; 122byte[] Logo 1{ 11get 11{3 1112Returns logo image bytes. 11132_logoBytes2null) 111{ 1111_logoBytes23File.2AllBytes3(3"Logo.png"3); 111} 1112 _logoBytes; 11} 1} }3

)f\[\,\4\2StreamReader, ReadLineStreamWriterReadAllTextReadAllLinescounts linesLINQ on filewrites array to fileWriteAllTextcaches binary file