C# StreamReader(string path) - 路径是否可以再次以某种方式访问?
C# StreamReader(string path) - Is the path accessible again somehow?
我有一个 StreamReader 的实现。
internal class MyStreamReader : StreamReader
{
public MyStreamReader (string path) : base(path)
{
}
参数是一个文件路径。
我想抛出异常并在消息中包含完整的文件名。我当然知道我可以轻松地将它保存在我自己的 class 字段中,例如“_path”,但我想确保我没有做任何多余的事情。我能以某种方式再次访问 "path" 参数吗?
编辑,因为似乎有些混乱:
throw new FileLoadException($"Corrupt source file! File '{ // I need something to represents the file with its path }' is malformed. (...)");
所以,// I need something to represent the file with its path
是否可以轻松替换而无需向我的 class 添加字段 _path
。类似于 this.GetTheInConstructorArgumentedPath()
。
尝试将 BaseStream 转换为 FileStream 并从中获取名称(如果您认为它始终是您使用的文件流)。
(this.BaseStream as FileStream).Name
我有一个 StreamReader 的实现。
internal class MyStreamReader : StreamReader
{
public MyStreamReader (string path) : base(path)
{
}
参数是一个文件路径。
我想抛出异常并在消息中包含完整的文件名。我当然知道我可以轻松地将它保存在我自己的 class 字段中,例如“_path”,但我想确保我没有做任何多余的事情。我能以某种方式再次访问 "path" 参数吗?
编辑,因为似乎有些混乱:
throw new FileLoadException($"Corrupt source file! File '{ // I need something to represents the file with its path }' is malformed. (...)");
所以,// I need something to represent the file with its path
是否可以轻松替换而无需向我的 class 添加字段 _path
。类似于 this.GetTheInConstructorArgumentedPath()
。
尝试将 BaseStream 转换为 FileStream 并从中获取名称(如果您认为它始终是您使用的文件流)。
(this.BaseStream as FileStream).Name