gpt4 book ai didi

c# - MemoryStream 到 string[]

转载 作者:太空狗 更新时间:2023-10-29 22:03:06 24 4
gpt4 key购买 nike

我从内存中的一个zip文件中读取一个CSV文件的内容(要求不写入磁盘)到MemoryStream中。并使用以下代码获取人类可读的字符串

 string  result = Encoding.ASCII.GetString(memoryStream.ToArray());

但是,我们希望结果是一个字符串 [] 来映射 CSV 文件中的每一行。

有没有办法自动处理这个问题?

谢谢

最佳答案

首先,不需要在内存流上调用ToArray。只需使用 StreamReader,并重复调用 ReadLine():

memoryStream.Position = 0; // Rewind!
List<string> rows = new List<string>();
// Are you *sure* you want ASCII?
using (var reader = new StreamReader(memoryStream, Encoding.ASCII))
{
string line;
while ((line = reader.ReadLine()) != null)
{
rows.Add(line);
}
}

关于c# - MemoryStream 到 string[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com