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!");




Reply With Quote