0

I have a code written in MFC for file reading and writing. I am rewriting it for C#.

My file consists of three parts, Header *Body* and Footer. Now in MFC code CArchive can write/read any of these parts. This is done by COleStreamFile::OpenStream. In this method we gave which part to read and this returns stream pointing to that location in the file. CArchive then uses stream and reads/writes to the file.

COleStreamFile stream;
//Stream is pointed to footer location.
stream.OpenStream(m_pStg, "Footer", nOpenFlags, pError);  // pStg is LPSTORAGE
CArchive ar(&stream, CArchive::load);

Now after this code when I do ar >> or ar << I didn't read file from start. It is reading from middle or end (depends on stream). Now what I want is that to convert this code to C#. Whats the replacement for COleStreamFile::OpenStream in C#.

Here is what I have done so far.

using (var stream = new FileStream(filePath, FileMode.Open))
{
    using (var binaryReader = new BinaryReader(stream)
    {
    }
}

Now here stream is pointing to the start. I think I can give it to read from specific byte. But I don't know that byte location. What I know is Header, Body and Footer names which are being used by MFC code.

Or is there any way to find out the current location of CArchive when it is reading or writing. If I get byte location from there I can use that as well.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • I think you would be better off keeping your code in C++ and writing a wrapper for C#. Otherwise you'll need to figure out what `COleStreamFile` is doing to point to each part of the file and mirror it in C#. – Dustin Kingen May 28 '13 at 11:50
  • Well we are getting rid of legacy code. And writing wrapper doesn't make sense in my case. – fhnaseer May 28 '13 at 11:52
  • Take a look at this answer for COM structured storage. [Is there any library to access OLE Structured Storage from C#?](http://stackoverflow.com/questions/2897328/is-there-any-library-to-access-ole-structured-storage-from-c). [Nuget Package](https://nuget.org/packages/OpenMcdf/) – Dustin Kingen May 28 '13 at 11:52

0 Answers0