c# – How to parse Stream


Is it possible to parse a stream? That is, get from it the file name, the path to the file, and what is in the file (in the form of text, of course)?


In general, there is no such information in streams. These entities provide the actual content of the resource (for example, a file) and some basic properties (for example, size, see the Length property – however, not all streams support this), as well as the ability to navigate and read. So all that you can pull out of the stream is its content itself.

If you have just a FileStream , then the full path to the file, including the name, can be taken from the Name property. The content is most conveniently read through StreamReader :

using (FileStream stream = ...)
using (var reader = new StreamReader(stream))
    string content = reader.ReadToEnd();
Scroll to Top