简体   繁体   English

C#如何将System.Net.ConnectStream转换为byte [](数组)

[英]C# How do i convert System.Net.ConnectStream to a byte[] (array)

im trying to convert a stream (System.Net.ConnectStream) to a byte array. 我试图将流(System.Net.ConnectStream)转换为字节数组。 Any thoughts/examples on how this can be done? 关于如何做到这一点的任何想法/例子?

Stream sourceStream = ... // the ConnectStream
byte[] array;
using (var ms = new MemoryStream())
{
    sourceStream.CopyTo(ms);
    array = ms.ToArray();
}

Try this... 试试这个...

    private static readonly object _lock = new object();

    public static byte[] readFullStream(Stream st)
    {
        try
        {
            Monitor.Enter(_lock);
            byte[] buffer = new byte[65536];
            Int32 bytesRead;
            MemoryStream ms = new MemoryStream();
            bool finished = false;
            while (!finished)
            {
                bytesRead = st.Read(buffer.Value, 0, buffer.Length);
                if (bytesRead > 0)
                {
                    ms.Write(buffer.Value, 0, bytesRead);
                }
                else
                {
                    finished = true;
                }
            }
            return ms.ToArray();
        }
        finally
        {
            Monitor.Exit(_lock);
        }
    }

In one answer from Freeetje there is method writen named 'ReadToEnd'. 在Freeetje的一个答案中,有一个名为'ReadToEnd'的方法。 Worked like a charm for me... 像我一样的魅力......

How do I get the filesize from the Microsoft.SharePoint.Client.File object? 如何从Microsoft.SharePoint.Client.File对象获取文件大小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM