@@ -86,7 +86,8 @@ public static (FileStream, FileStream) OpenReadFile()
8686
8787 //Open a read-only file stream with instance FileInfo.OpenRead()
8888 FileInfo fileInfo = new ( "myFileTwo.txt" ) ;
89- if ( ! fileInfo . Exists ) fileInfo . Create ( ) ;
89+ if ( ! fileInfo . Exists )
90+ fileInfo . Create ( ) . Dispose ( ) ;
9091
9192 using FileStream openReadFileInfoResult = fileInfo . OpenRead ( ) ;
9293
@@ -121,13 +122,12 @@ public static string CreateReadAndWriteViaFile()
121122 using StreamWriter streamWriter = File . CreateText ( "sampleDocument.txt" ) ;
122123 //To write to the file
123124 streamWriter . WriteLine ( "My Todo" ) ;
124- Console . WriteLine ( ) ;
125125 streamWriter . WriteLine ( "Drink Water" ) ;
126126 streamWriter . WriteLine ( "Be Awesome" ) ;
127127 }
128128 //To open and read from the created .txt file
129129 using StreamReader streamReader = File . OpenText ( "sampleDocument.txt" ) ;
130- string result ;
130+ string ? result ;
131131
132132 StringBuilder sr = new ( ) ;
133133 while ( ( result = streamReader . ReadLine ( ) ) != null )
@@ -153,7 +153,7 @@ public static string CreateReadAndWriteViaFileInfo()
153153 }
154154 //To open and read from the created .txt file
155155 using StreamReader streamReader = fileInfo . OpenText ( ) ;
156- string result ;
156+ string ? result ;
157157
158158 StringBuilder sr = new ( ) ;
159159 while ( ( result = streamReader . ReadLine ( ) ) != null )
@@ -164,33 +164,69 @@ public static string CreateReadAndWriteViaFileInfo()
164164 return sr . ToString ( ) ;
165165 }
166166
167- public static void RefreshMethodOfFileInfo ( )
167+ public static ( bool BeforeRefresh , bool AfterRefresh ) RefreshMethodOfFileInfo ( )
168168 {
169169 FileInfo fileInfo = new ( "myFileFifteen.txt" ) ;
170+ Console . WriteLine ( fileInfo . Exists ) ; // False, and now cached
171+
172+ File . WriteAllText ( "myFileFifteen.txt" , "Created behind our back" ) ;
173+ bool beforeRefresh = fileInfo . Exists ;
174+ Console . WriteLine ( beforeRefresh ) ; // still False
170175
171- fileInfo . Attributes = FileAttributes . Hidden ;
172176 fileInfo . Refresh ( ) ;
173- Console . WriteLine ( fileInfo . Attributes . ToString ( ) ) ;
177+ bool afterRefresh = fileInfo . Exists ;
178+ Console . WriteLine ( afterRefresh ) ; // True
179+
174180 fileInfo . Delete ( ) ;
181+
182+ return ( beforeRefresh , afterRefresh ) ;
183+ }
184+
185+ public static string OpenWriteKeepsTheTail ( )
186+ {
187+ File . WriteAllText ( "myFileSeventeen.txt" , "This is a long line" ) ;
188+
189+ using ( FileStream stream = File . OpenWrite ( "myFileSeventeen.txt" ) )
190+ {
191+ stream . Write ( "Short"u8 ) ;
192+ }
193+
194+ string content = File . ReadAllText ( "myFileSeventeen.txt" ) ;
195+ Console . WriteLine ( content ) ; // Shortis a long line
196+ File . Delete ( "myFileSeventeen.txt" ) ;
197+
198+ return content ;
175199 }
176200
177201 public static void UsingFileOnMultipleFiles ( )
178202 {
203+ File . WriteAllText ( "myFileSix.txt" , "My Todo" ) ;
204+
179205 File . Copy ( "myFileSix.txt" , "myFileSeven.txt" ) ;
180- File . Encrypt ( "myFileSix.txt" ) ;
181- File . Decrypt ( "myFileSix.txt" ) ;
182- File . Move ( "myFileSeven.txt" , "myFileSix.txt" ) ;
206+ File . AppendAllText ( "myFileSeven.txt" , " list" ) ;
207+ File . Move ( "myFileSeven.txt" , "myFileEight.txt" ) ;
183208 File . Delete ( "myFileSix.txt" ) ;
184- File . Delete ( "myFileSeven .txt" ) ;
209+ File . Delete ( "myFileEight .txt" ) ;
185210 }
186211
187- public static void UsingFileInfoOnOneFile ( )
212+ public static string UsingFileInfoOnOneFile ( )
188213 {
189214 FileInfo fileInfo = new ( "myFileSixteen.txt" ) ;
190- fileInfo . OpenText ( ) ;
191- fileInfo . Encrypt ( ) ;
192- fileInfo . Decrypt ( ) ;
215+
216+ using ( StreamWriter writer = fileInfo . CreateText ( ) )
217+ {
218+ writer . Write ( "My Todo" ) ;
219+ }
220+
221+ Console . WriteLine ( $ "{ fileInfo . Name } : { fileInfo . Length } bytes") ;
222+
223+ fileInfo . MoveTo ( "myFileSixteenMoved.txt" ) ;
224+ Console . WriteLine ( fileInfo . Name ) ; // myFileSixteenMoved.txt
225+
226+ string name = fileInfo . Name ;
193227 fileInfo . Delete ( ) ;
228+
229+ return name ;
194230 }
195231 }
196232}
0 commit comments