OddDev OddDev - 14 days ago 10
C# Question

C# StreamReader(string path) - Is the path accessible again somehow?

I've an implementation of StreamReader.

internal class MyStreamReader : StreamReader
{
public MyStreamReader (string path) : base(path)
{
}


The argument is a path to a file.

I wanted to throw an exception and include the complete filename in its message. I know of course that I could easily save it in a field of my own class like "_path" but I wanted to make sure that I don't do anything redundant. Am I somehow able to access the "path" argument again?

Edit since there seems to be some confusion:

throw new FileLoadException($"Corrupt source file! File '{ // I need something to represents the file with its path }' is malformed. (...)");


So, is
// I need something to represent the file with its path
easily replaceable without adding a field
_path
to my class. Something like
this.GetTheInConstructorArgumentedPath()
.

Answer

Try casting BaseStream to FileStream and get name from it (if you think it is always a file stream you use).

(this.BaseStream as FileStream).Name