Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 16th, 2007, 12:17 PM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deserialization of a file

I have a file with an object serialized with System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter. To ensure that the file is written properly, I append a 64 character checksum to the end. I can pull out and verify the checksum, but I can't figure out how to get the BinaryFormatter to deserialize the data. I can't have it deserialize the file stream, because it has the checksum at the end.

If I:
  • Open the file
  • Read it with a StreamReader
  • Output the stream to a string
  • Remove the checksum
  • Convert the string to a byte[] using System.Text.Encoding.Ascii.GetBytes()
  • Put the bytes into a MemoryStream
  • Deserialize the MemoryStream
I get this error:
Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
  #2 (permalink)  
Old July 16th, 2007, 02:08 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

What about manually reading in the stream, removing the last 64 bytes then deserialize?

  #3 (permalink)  
Old July 16th, 2007, 08:11 PM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

But that's what I'm doing. Here is my entire method:

private void loadChecksummedFile(object obj, string fileExension)
    FileStream fs = new FileStream(m_filePath + "." + fileExension, FileMode.Open, FileAccess.ReadWrite);
    BinaryFormatter bf = new BinaryFormatter();
    StreamReader sr = new StreamReader(fs);
    MemoryStream ms;
    MD5 checksum = MD5.Create();
    string fileString;
    string checksumInFile;
    byte[] data;
    string newChecksum;

    fileString = sr.ReadToEnd(); // Read the file into a string
    checksumInFile = fileString.Substring(fileString.Length - 64, 64); // Pull out the checksum
    data = System.Text.Encoding.ASCII.GetBytes(fileString.Substring(0, fileString.Length - 64)); //Take the string (minus the 
                                                                                                //checksum) and put it in a byte[]
    // Compute the checksum and compare
    newChecksum = checksumToString(checksum.ComputeHash(data)); 
    if(checksumInFile == newChecksum)
        ms = new MemoryStream(data); //Put the byte[] of data into a memory stream because BinaryFormatter can only Deserialize 
        obj = (object[]) bf.Deserialize(ms); //Error occurs here
        //TODO: Load the backup



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialization and DeSerialization balesh.mind ASP.NET 2.0 Professional 0 September 12th, 2007 05:05 AM
Serialization & Deserialization kadimijagan Pro Java 2 September 7th, 2007 10:13 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
How to display .tif file, .pdf file and .jpg file phuong171 ASP.NET 1.0 and 1.1 Basics 1 March 13th, 2007 07:29 AM

All times are GMT -4. The time now is 05:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.