none
How to copy File stream to Memory Stream RRS feed

  • Question

  • Hi,

    I'm having File stream which is saved in Temp. and now i want to return the stream and delete the Temp file.

    So i have a plan to copy all the file stream to Memory stream and delete the temp file.

    Please help me to proceed.

    Thursday, February 20, 2014 10:17 AM

Answers

  • But that doesn't make sense. Just write the original input stream to the memory stream instead of writing it to the temp file. You gain nothing if you write it first to the temp file only to read that temp file back.

    Just replace

    outputStream = new FileStream(path, FileMode.Create);

    with

    outputStream = new MemoryStream();

    and get rid of

    string path = Path.GetTempFileName();

    Granted, the current code isn't exactly efficient because it reads/writes only one byte at a time but first let's settle on a working solution.

    Thursday, February 20, 2014 3:08 PM
    Moderator

All replies

  • So read the file stream in a byte[] and the create a memory stream from the byte array:

    MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(filePath));

    Thursday, February 20, 2014 10:29 AM
    Moderator
  • Thanks for ur input.

    since i'm new to this. please give the detailed code.

               string path = Path.GetTempFileName();
                FileStream outputStream = null;
                try
                {
                    int streamLength, length, padLength;
                    byte padValue = 0;
                    int intByte;

    outputStream = new FileStream(path, FileMode.Create);

                    do
                    {
                        intByte = inputStream.ReadByte();
                        if (intByte != -1)
                        {
                            padValue = (byte)intByte;
                            outputStream.WriteByte(padValue);

                        }
                    } while (intByte != -1);
                    streamLength = (int)outputStream.Length;
                    //
                    //Get padding value and length of padding
                    padLength = (int)padValue + 1;
                    length = streamLength - padLength;
                    outputStream.SetLength(length);
                    outputStream.Position = 0;

                   ==================================

    I need to copy the output stream to memory stream and need to delete the tmp.

    please do the needful

    Thursday, February 20, 2014 10:35 AM
  • I don't understand what you are trying to do. Originally you said that you have a file and you want to load it into a MemoryStream. I showed you how to do that.

    But the code you have posted does something else. You seem to have an inputStream which you copy to a file and now it seems that you want to load that file into a MemoryStream. What's the point of that, just copy the inputStream to a MemoryStream directly.

    Thursday, February 20, 2014 10:43 AM
    Moderator
  • Thanks for ur input.

    since i'm new to this. please give the detailed code.

               string path = Path.GetTempFileName();
                FileStream outputStream = null;
                try
                {
                    int streamLength, length, padLength;
                    byte padValue = 0;
                    int intByte;

    outputStream = new FileStream(path, FileMode.Create);

                    do
                    {
                        intByte = inputStream.ReadByte();
                        if (intByte != -1)
                        {
                            padValue = (byte)intByte;
                            outputStream.WriteByte(padValue);

                        }
                    } while (intByte != -1);
                    streamLength = (int)outputStream.Length;
                    //
                    //Get padding value and length of padding
                    padLength = (int)padValue + 1;
                    length = streamLength - padLength;
                    outputStream.SetLength(length);
                    outputStream.Position = 0;

                   ==================================

    I need to copy the output stream to memory stream and need to delete the tmp.

    please do the needful

    Hi, see below, read the temp file to memory stream and delete the file

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(path));
    System.IO.File.Delete(path);

    Thursday, February 20, 2014 10:56 AM
  • Hi,

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(path));
    System.IO.File.Delete(path);

    the above is for file. I'm asking for the file stream.

    please do the need ful.

    Thursday, February 20, 2014 12:38 PM
  • Hi,

    I'm having one stream and i'm writing the stream into the filestream(in temp file)

    now i need to return the file stream and parally i need to delete the temp file.

    since the file stream is returned and i couldn't delete the temp file

    Thursday, February 20, 2014 12:53 PM
  • But that doesn't make sense. Just write the original input stream to the memory stream instead of writing it to the temp file. You gain nothing if you write it first to the temp file only to read that temp file back.

    Just replace

    outputStream = new FileStream(path, FileMode.Create);

    with

    outputStream = new MemoryStream();

    and get rid of

    string path = Path.GetTempFileName();

    Granted, the current code isn't exactly efficient because it reads/writes only one byte at a time but first let's settle on a working solution.

    Thursday, February 20, 2014 3:08 PM
    Moderator