ReadToEnd后关闭的StreamReaderReadToEnd、StreamReader

2023-09-04 08:46:06 作者:没有刘海看清了世界。

是否有可能以某种方式调用ReadToEnd方法在建设这样后关闭StreamReader的:

Is it possible somehow to close StreamReader after calling ReadToEnd method in construction like this:

string s = new StreamReader("filename", Encoding.UTF8).ReadToEnd();

任何替代优雅的建筑,相同的语法也将被接受。

Any alternative elegant construction with the same semantics will be also accepted.

推荐答案

我觉得你的方法真的就是后的 File.ReadAllText ,如果​​你只是想读在最短​​的code文件中的所有文本。

I think the method you're really after is File.ReadAllText, if you're just trying to read all the text from a file in the shortest possible code.

如果您不指定编码,将使用UTF-8会自动。

If you don't specify the encoding, it will use UTF-8 automatically.