开发者

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

开发者 https://www.devze.com 2023-03-22 06:51 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
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

精彩评论

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

关注公众号