FileStream

Use the FileStream class to read from, write to, open, and close files on a file system
This is one of the classes derived from the Stream class.
File streams are buffered
Data is not written to a file when you write to the FileStream.


BeginReadstart an asynchronous operation on the stream
BeginWritestart an asynchronous operation on the stream
CloseCloses the stream
EndReadTo find out how many bytes were actually written
FlushEmpties the buferred stream and ensures all the contents are written
HandleReturns the operating system file handle
LengthReturns the total size of the stream. You can change the length using SetLength
LockLock a portion of the file
Positiondetermines the current position in the stream. You can change the position using Seek.
ReadReads a number of bytes from the specified position into a Byte array. The ReadByte method reads and returns a single byte.
SetLengthTrims or extends the underlying file.
UnlockUnlock a portion of the file
WriteWrites a number of bytes from an array into the stream. The WriteByte can write a single byte.


You can check which operations are allowed by using the CanRead, CanWrite and CanSeek properties.

System.IO.FileStream oFileStream; 
oFileStream = System.IO.FileStream("C:\Temp\textfile.txt",
    System.IO.FileMode.Open,
    System.IO.FileAccess.Read,
    System.IO.FileShare.Read);

   if (oFileStream.CanRead == true)
   {
   }

System.IO.FileMode.Append | Create | CreateNew | Open | OpenOrCreate | Truncate
System.IO.FileAccess.Read | Write | ReadWrite
System.IO.FileShare.None | ReadWrite | Read | Write | Inheritable


Using File.OpenRead

This is another way of obtaining a FileStream object.

System.IO.FileStream oFileStream; 
oFileStream = System.IO.File.OpenRead("C:\Temp\textfile.txt");


Stream Base Class

System.IO.Stream




© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext