, );
For example : Create a FileStream object F to read a file with the name vidu.txt, as follows:
FileStream F = new FileStream(" vidu.txt ", FileMode.Open, FileAccess.Read,ParameterDescriptionFileMode
FileShare.Read);
The FileMode enumerator defines different methods to open the file. The members of the FileMode enumerator are:
Append : Open an existing file and place the cursor at the end of the file, or create a File, if the file doesn't already exist.
Create : Create a new file.
CreateNew : Determine with the operating system that it will create a new file.
Open : Open an existing file.
OpenOrCreate : Determine with the operating system that it will open a file if it exists, otherwise it will create a new file.
Truncate : Open an existing file and truncate its size to 0 bytes.
FileAccess enumerators have members: Read , ReadWrite and Write .
FileShareFileShare enumerators have the following members:
Inheritable : Allows a traditional file to inherit to child processes.
None : Reject the current file sharing.
Read : Allow to open the file to read.
ReadWrite : Allows opening files to read and write.
Write : Allows you to open files for recording.
For example:
Here is an example to illustrate how to use the FileStream class in C #:
using System ; using System . IO ; namespace VdIO { class Program { static void Main ( string [] args ) { FileStream F = new FileStream ( "test.dat" , FileMode . OpenOrCreate , FileAccess . ReadWrite ); for ( int i = 1 ; i <= 20 ; i ++) { F . WriteByte (( byte ) i ); } F . Position = 0 ; for ( int i = 0 ; i <= 20 ; i ++) { Console . Write ( F . ReadByte () + " " ); } F . Close (); Console . ReadKey (); } } }
Use the Console.ReadKey command (); to see the results more clearly. Compiling and running the above C # program will produce the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
The previous example illustrates simple operations on files in C #. However, to take full advantage of the System.IO classes in C #, you need to know the commonly used properties and methods for these classes.
Follow tutorialspoint
Previous article: Handling exceptions (Try / Catch / Finally) in C #
Next lesson: Attribute in C #