开发者

What is the most robust way to open file and read content [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this 开发者_如何学运维question so that it can be reopened, visit the help center. Closed 11 years ago.

I usually do this when I have to read the whole file always:

using (var fileStream = File.OpenRead(...))
  using (var reader = new StreamReader(fileStream))
     var content = reader.ReadToEnd();

Any better/faster way?


string content = System.IO.File.ReadAllText(@"your file path");

If you want to get lines:

string[] lines = System.IO.File.ReadAllLines(@"your file path");

if you want to only read lines until get to some line then use the IEnumerable ReadLines:

foreach(string line in System.IO.File.ReadLines(@"your file path")
{
    if (line == ...)
    {
        break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜