Filestream C

4/28/2019
15 Comments
Filestream C Rating: 7,5/10 4723 reviews

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

C# I/O Classes

The System.IO namespace has various classes that are used for performing numerous operations with files, such as creating and deleting files, reading from or writing to a file, closing a file etc.

The following table shows some commonly used non-abstract classes in the System.IO namespace −

Sr.No.I/O Class & Description
1

BinaryReader

Reads primitive data from a binary stream.

2

BinaryWriter

Writes primitive data in binary format.

3

BufferedStream

A temporary storage for a stream of bytes.

4

Directory

Helps in manipulating a directory structure.

5

DirectoryInfo

Used for performing operations on directories.

6

DriveInfo

Provides information for the drives.

7

File

Helps in manipulating files.

8

FileInfo

Used for performing operations on files.

9

FileStream

Used to read from and write to any location in a file.

10

MemoryStream

Used for random access to streamed data stored in memory.

11

Path

Performs operations on path information.

12

StreamReader

Used for reading characters from a byte stream.

13

StreamWriter

Is used for writing characters to a stream.

14

StringReader

Is used for reading from a string buffer.

15

StringWriter

Is used for writing into a string buffer.

The FileStream Class

The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream.

You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows −

For example, we create a FileStream object F for reading a file named sample.txt as shown

Sr.No.Parameter & Description
1

FileMode

The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −

  • Append − It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.

  • Create − It creates a new file.

  • CreateNew − It specifies to the operating system, that it should create a new file.

  • Open − It opens an existing file.

  • OpenOrCreate − It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.

  • Truncate − It opens an existing file and truncates its size to zero bytes.

    Lenovo change serial number. It is intended for manufacturers to be able to change text strings stored in BIOS. Error 00CE: Machine Type and Serial Number are INVALID. Jan 23, 2018 - Lenovo devices, generate double identifier and change the serial number, assuming they are two different machines, when Bios is updated,. If you change a system board on a Thinkpad laptop, chances are you might encounter 2200, 2201 errors. 2200: Machine Type and Serial Number are invalid. Jul 11, 2016 - How to flash the machine type and model (MTM), serial number. Menu, it can be corrected with utilities included in the system BIOS update package. From the Lenovo support site (search for the. Hi guys, recently i bought one lenovo Carbon X1 off the auction and its motherboard was faulty and mtm, serial etc label have been taken off.

2

FileAccess

FileAccess enumerators have members: Read, ReadWrite and Write.

3

FileShare

FileShare enumerators have the following members −

Charlie then visits the Cullen house and meets Renesmee, believing she is a niece of Edward's whom Bella and Edward adopted. He then phases into his wolf form. Breaking dawn part 2.

  • Inheritable − It allows a file handle to pass inheritance to the child processes

  • None − It declines sharing of the current file

  • Read − It allows opening the file for readin.

  • ReadWrite − It allows opening the file for reading and writing

  • Write − It allows opening the file for writing

Example

The following program demonstrates use of the FileStream class −

Live Demo

When the above code is compiled and executed, it produces the following result −

Advanced File Operations in C#

The preceding example provides simple file operations in C#. However, to utilize the immense powers of C# System.IO classes, you need to know the commonly used properties and methods of these classes.

Sr.No.Topic & Description
1Reading from and Writing into Text files

It involves reading from and writing into text files. The StreamReader and StreamWriter class helps to accomplish it.

2Reading from and Writing into Binary files

It involves reading from and writing into binary files. The BinaryReader and BinaryWriter class helps to accomplish this.

3Manipulating the Windows file system

It gives a C# programamer the ability to browse and locate Windows files and directories.