How to get a MemoryStream from a Stream in .NET?

后端 未结 9 1060
天命终不由人
天命终不由人 2020-12-02 16:16

I have the following constructor method which opens a MemoryStream from a file path:

MemoryStream _ms;

public MyClass(string filePath)
{
    by         


        
相关标签:
9条回答
  • 2020-12-02 16:57
    byte[] fileData = null;
    using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
    {
        fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
    }
    
    0 讨论(0)
  • 2020-12-02 17:02

    Use this:

    var memoryStream = new MemoryStream();
    stream.CopyTo(memoryStream);
    

    This will convert Stream to MemoryStream.

    0 讨论(0)
  • 2020-12-02 17:03

    I use this combination of extension methods:

        public static Stream Copy(this Stream source)
        {
            if (source == null)
                return null;
    
            long originalPosition = -1;
    
            if (source.CanSeek)
                originalPosition = source.Position;
    
            MemoryStream ms = new MemoryStream();
    
            try
            {
                Copy(source, ms);
    
                if (originalPosition > -1)
                    ms.Seek(originalPosition, SeekOrigin.Begin);
                else
                    ms.Seek(0, SeekOrigin.Begin);
    
                return ms;
            }
            catch
            {
                ms.Dispose();
                throw;
            }
        }
    
        public static void Copy(this Stream source, Stream target)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (target == null)
                throw new ArgumentNullException("target");
    
            long originalSourcePosition = -1;
            int count = 0;
            byte[] buffer = new byte[0x1000];
    
            if (source.CanSeek)
            {
                originalSourcePosition = source.Position;
                source.Seek(0, SeekOrigin.Begin);
            }
    
            while ((count = source.Read(buffer, 0, buffer.Length)) > 0)
                target.Write(buffer, 0, count);
    
            if (originalSourcePosition > -1)
            {
                source.Seek(originalSourcePosition, SeekOrigin.Begin);
            }
        }
    
    0 讨论(0)
  • 2020-12-02 17:05

    How do I copy the contents of one stream to another?

    see that. accept a stream and copy to memory. you should not use .Length for just Stream because it is not necessarily implemented in every concrete Stream.

    0 讨论(0)
  • 2020-12-02 17:08

    You can simply do:

    var ms = new MemoryStream(File.ReadAllBytes(filePath));
    

    Stream position is 0 and ready to use.

    0 讨论(0)
  • 2020-12-02 17:11

    In .NET 4, you can use Stream.CopyTo to copy a stream, instead of the home-brew methods listed in the other answers.

    MemoryStream _ms;
    
    public MyClass(Stream sourceStream)
    
        _ms = new MemoryStream();
        sourceStream.CopyTo(_ms);
    }
    
    0 讨论(0)
提交回复
热议问题