对于下面的代码片段中file.Close()语句在.net core情况下提示 FileStream未包含“Close”的定义,但是在 .net framework中就不提示错误。
那位大佬能回答下是怎么回事?
var file = File.Open(txtpath, FileMode.Open);
List<string> txt = new List<string>();
using (var stream = new StreamReader(file, System.Text.Encoding.GetEncoding("gb2312")))
{
while (!stream.EndOfStream)
{
txt.Add(stream.ReadLine());
}
}
file.Close();
我用vs2017、.net core2.1看了没有你说的情况,另外代码需要改进(放在using里面就不需要Close方法):
List<string> txt = new List<string>();
using (var file = File.Open(txtpath, FileMode.Open))
{
var stream = new StreamReader(file, System.Text.Encoding.GetEncoding("gb2312"));
while (!stream.EndOfStream)
{
txt.Add(stream.ReadLine());
}
}
或者更简单点:
List<string> txt = new List<string>();
txt.AddRange(File.ReadAllLines(txtpath, System.Text.Encoding.GetEncoding("gb2312")));
using的是哪个namespace下的FileStream
检查下