FileStream and BinaryFormatter

Posted on 2005-05-14
Last Modified: 2008-03-16
I use FileStream to save my project (Calculation) to hard drive. It works fine. Problem is when I change something in Calculation class that it can not read it again because of different number of items in it. What could I do to make old versions readable?

        BinFormatter.Serialize(FStream, actCalcForm.actCalculation)

Question by:svemir
    LVL 96

    Expert Comment

    by:Bob Learned
    This, I believe, is the inherent problem with serialization, and makes it an unusable form of persistence as far as I am concerned.  Where are you storing this data?  What other information can you give me that would help me make a recommendation?


    Author Comment

    actually it can be stored where ever user want to. One way to solve this problem is to keep old classes a live inside of application and call it when needed. In that case I need to write additional source code to rewrite informations to new class. Usually it is not a problem because most of inherited classes in main class which I serialize are same. Anyway it seams to me less complicated than old fasion storing data.

    I hope that you have better idea about my question.

    Thank you anyway.
    LVL 30

    Expert Comment

    by:Alexandre Simões
    I'm sorry (as I had anything to do with it! :)) but you just can't.

    Binary serialization doen't support different versions, only XML.
    If you have a choice, serialize the Calculation class to XML instead of binaly.

    The problem can be the security, XML is pretty much human readable.
    To overcome this you can:
    1- If you really need a very good protection, ecrypt the XML file, using DES or TDES implementes on the System.Security.Cryptography namespace.
    2- If you just want put some camuflage on the file just don't save the file as *.xml, choose another file extension :)

    Alex :p

    Author Comment

    Could you please give some example of serialization to XML because once I try and it doesn't work.
    LVL 30

    Accepted Solution

    Be carefull with the properties types... all must be serializable.

    Something like this:

        Public Sub SerializeMyClass(ByVal Class2Ser As mySerClass)

            Dim fs As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath & "\Serialized.xml", IO.FileMode.Create)
            Dim serializer As System.Xml.Serialization.XmlSerializer

            serializer.Serialize(fs, Class2Ser)

        End Sub

        Public Function DeserializeMyClass() As mySerClass

            Dim fs As New System.IO.FileStream(System.Windows.Forms.Application.StartupPath & "\Serialized.xml", IO.FileMode.Open)
            Dim serializer As System.Xml.Serialization.XmlSerializer

            Dim retval As Object = serializer.Deserialize(fs)

            Return retval
        End Function

    End Module

    <Serializable()> Public Class mySerClass

        Public Sub New()
            'To be serializable, the class must have a contructor with no parameters.
        End Sub

        '-> Only public properties are serialized, sub's and
        '       functions are ignored.
        '-> If you want XML Serializer to ignore a public
        '       property just add <Xml.Serialization.XmlIgnore()>
        '       before the property.
        Private m_P1 As Integer
        Public Property P1() As Integer
                Return P1
            End Get
            Set(ByVal Value As Integer)
                P1 = Value
            End Set
        End Property

    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now