0
FileStream myFileStream = File.Open("C:\\file.txt", FileMode.Open);

This line declares and initializes a variable myFileStream, but what is exactly in this variable? Does it contain all the bytes from "file.txt"? Or does it contain info about an established connection with the file?

4 Answers4

1

It contains information about the established connection with the file. Now when the connection is established and the file is opened, you can manipulate the text inside the file.

GeekDev
  • 11
  • 3
1

The variable myFileStream is just a HANDLE. Nothing more.

You can use this HANDLE to access the file's contents or modify the the file's permissions with other Methods/Functions.

On the binary level it's nothing more than a 32-bit/64-bit number used by the Operating System to reference to that file for being used by other Methods/Functions.

So a simple answer to your question

Does a FileStream contain all the file's data?

is that it contains no data, but represents a HANDLE to access all of the file's data.

Community
  • 1
  • 1
zx485
  • 28,498
  • 28
  • 50
  • 59
1

As from Wikipedia definition In computer science, a stream is a sequence of data elements made available over time

So your variable myFileStream is just a pointer that will allow you to manipulate a large amount of data

may be you have to look for the difference between Stream and Buffer

A buffer is normally just a block of memory where things can be stored in RAM.

A stream is something that lets you store things on disk, send across to other computers such as the internet, serial port, UCB, etc. streams often use buffers to optimize transmission speed.

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

The FileStream object represents a handle to a representation of the file. It acts as a reference to some underlying structure that is handled by your operating system which allows your code to ask the operating system for operations on the file that resides in your file system, such as Read, Write and Seek.

What is important to keep in mind is that Streams in general are abstract constructs (from the MSDN page on Streams) that are designed for providing a clean interface to common operations on concepts that are similar, for example a File or a Socket.

Khaled Nassar
  • 884
  • 8
  • 23