51

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"
Abel
  • 56,041
  • 24
  • 146
  • 247
Ram
  • 901
  • 5
  • 18
  • 27
  • 4
    Pretty similar to this: http://stackoverflow.com/questions/6200329/how-to-load-a-file-from-folder-to-memory-stream-buffer – Tridus Jun 02 '11 at 11:37

4 Answers4

95
Dim stream As New MemoryStream(File.ReadAllBytes(filename))
Centro
  • 3,892
  • 2
  • 25
  • 31
  • after loading a file into memory stream can i add titles to the content of the file? like headers in xls file. – Ram Jun 02 '11 at 11:43
50

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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();
        }
    }
Developer63
  • 640
  • 7
  • 19
-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();  
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • 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 07 '17 at 15:44