小编Vic*_*sel的帖子

StreamReader ReadToEnd()在第一次尝试时返回空字符串

我知道之前在Stackoverflow上已经问过这个问题,但是找不到解释.

当我尝试从压缩字节数组中读取一个字符串时,我在第一次尝试时得到一个空字符串,在第二次尝试时我得到了字符串.

代码示例:

public static string Decompress(byte[] gzBuffer)
{
    if (gzBuffer == null)
        return null;
    using (var ms = new MemoryStream(gzBuffer))
    {
        using (var decompress = new GZipStream(ms, CompressionMode.Decompress))
        {
            using (var sr = new StreamReader(decompress, Encoding.UTF8))
            {
                string ret = sr.ReadToEnd();
                // this is the extra check that is needed !?
                if (ret == "")
                    ret = sr.ReadToEnd();
                return ret;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所有建议表示赞赏. - Victor Cassel

c# streamreader

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

streamreader ×1