50

I have a filename pointing to a text file, including its path, as a string. Now I'd like to load this .csv file into memory stream. How should I do that?

For example, I have this:

Dim filename as string="C:\Users\Desktop\abc.csv"
1

4 Answers 4

94
Dim stream As New MemoryStream(File.ReadAllBytes(filename))
1
  • after loading a file into memory stream can i add titles to the content of the file? like headers in xls file.
    – Ram
    Jun 2, 2011 at 11:43
49

You don't need to load a file into a MemoryStream.

You can simply call File.OpenRead to get a FileStream containing the file.

If you really want the file to be in a MemoryStream, you can call CopyTo to copy the FileStream to a MemoryStream.

4
  • can i append the file while itis in filestream?
    – Ram
    Jun 2, 2011 at 11:44
  • @Ram: Yes, you can. Just call Open rather than OpenRead. You can then write and read the same stream.
    – SLaks
    Jun 2, 2011 at 11:45
  • CopyTo method exists from .NET 4 onwards. See this answer stackoverflow.com/a/5730893/1589759 Apr 11, 2016 at 15:12
  • 6
    This shouldn't be the answer, it's not what the op asked. Oct 4, 2018 at 10:14
0

I had an XML file being read from disk, using the old XmlReader API. How to read the XML file into memory, and then work with it in memory, instead of reading the disk repeatedly? Based on VB answer from Centro (upvoted) but with a Using block, and in C#.

The key line:

MemoryStream myXMLDocument = new MemoryStream(File.ReadAllBytes(@"c:\temp\myDemoXMLDocument.xml"));

Re the OP's question, if you wanted to load a CSV file into a MemoryStream:

MemoryStream myCSVDataInMemory = new MemoryStream(File.ReadAllBytes(@"C:\Users\Desktop\abc.csv"));

Following is a code snippet showing code to reads through XML document now that it's in a MemoryStream. Basically the same code as when it was coming from a FileStream that pointed to a file on disk. Yes, the XMLTextReader API is old and clunky, but it's what I had to work with in this app.

    string myXMLFileName = @"c:\temp\myDemoXMLDocument.xml";
    using (MemoryStream myXMLDocument = new MemoryStream(File.ReadAllBytes(myXMLFileName)))
    {
            myXMLTextReader = new XmlTextReader(myXMLDocument);
            myXMLTextReader.WhitespaceHandling = WhitespaceHandling.None;
            myXmlTextReader.Read(); // read the XML declaration node, advance to <Batch> tag
            while (!myXmlTextReader.EOF) 
            {
                if (myXmlTextReader.Name == "xml" && !myXmlTextReader.IsStartElement()) break;
                // advance to <Batch> tag
                while (myXmlTextReader.Name == "Batch" && myXmlTextReader.IsStartElement())
                {
                    string BatchIdentifier = myXmlTextReader.GetAttribute("BatchIdentifier");
                    myXmlTextReader.Read(); // advance to next tag
                    while (!myXmlTextReader.EOF)
                    {
                        if (myXmlTextReader.Name == "Transaction" && myXmlTextReader.IsStartElement())
                        {
                            // Start a new set of items
                            string transactionID = myXmlTextReader.GetAttribute("ID");
                        myXmlTextReader.Read(); // Read next element, possibly another Transaction tag
                    }
                }
                //All Batch tags are completed.Move to next tag
                myXmlTextReader.Read();
            }
            // Close the XML memory stream.
            myXmlTextReader.Close();
            myXmlDocument.Close();
        }
    }
-1

You can copy it to a file stream like so:

string fullPath = Path.Combine(filePath, fileName);
FileStream fileStream = new FileStream(fullPath, FileMode.Open);
Image image = Image.FromStream(fileStream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
//Close File Stream
fileStream.Close();  
1
  • Doing it this way will decompress the image then recompress it as jpeg. Just use the file stream copyto method to copy the byte array to the memory stream.
    – hcham1
    May 7, 2017 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.