ReadAllLines for a Stream object?

后端 未结 6 814
醉酒成梦
醉酒成梦 2020-12-01 11:31

There exists a File.ReadAllLines but not a Stream.ReadAllLines.

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestR         


        
6条回答
  •  情书的邮戳
    2020-12-01 12:21

    Using the following extension method:

    public static class Extensions
    {
        public static IEnumerable ReadAllLines(this StreamReader reader)
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
        }
    }
    

    It's possible to get to your desired code:

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt"))
    using (StreamReader reader = new StreamReader(stream))
    {
        string[] result = reader.ReadAllLines().ToArray();
    }
    

提交回复
热议问题