|
-
Feb 11th, 2005, 01:46 PM
#1
Thread Starter
Junior Member
Reading files into a memory stream.
Ok, so I am having my program open a file into a memory stream and then write it back to another file. I'm not sure if this is the best way to do it for both speed and optimized memory usage (the code not the idea of opening the file into memory). the only problem I am actually having is that the memory stream doesn't seem to go away. After it runs, I look in the task manager and its still taking up 100 megs of memory.
Code:
FileStream fs;
MemoryStream ms;
ms = new MemoryStream();
fs = new FileStream(ofdOpen.FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
FileStream fs2;
fs2 = new FileStream(Application.StartupPath + @"\tmp.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs2);
int blockSize = 4096;
int bytesRead;
byte[] buffer = new byte[blockSize];
fs.Position = 0;
while (!(fs.Position == fs.Length))
{
bytesRead = fs.Read(buffer, 0, blockSize);
ms.Write(buffer, 0, bytesRead);
}
ms.Position = 0;
while (!(ms.Position == ms.Length))
{
bytesRead = ms.Read(buffer, 0, blockSize);
bw.Write(buffer);
}
ms.Flush();
ms.Close();
MessageBox.Show("Done!");
Last edited by farklem; Feb 11th, 2005 at 01:51 PM.
-
Feb 14th, 2005, 03:48 AM
#2
Re: Reading files into a memory stream.
Call Dispose on it, set it null and then call GC.Collect
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|