AllenD's WebLog




Solution Options Persistence (Part 2)

It occured to me that I didn't discuss what the framework will do when the options you specified in the AddOptionsKey method are needed to be loaded or saved.

The framework will eventually call one of two virtual methods that you need to override.

        protected override void OnLoadOptions(string key, Stream stream) {
            // if key is a string this package cares about then
            // stream can be used to read the data for runtime use
            if (key == MyOptionKey)
                try {
                    BinaryReader reader = new BinaryReader(stream);
                    // read the options here...
                } catch (Exception) {
                    // no options, ok, no problem.
                }
            } else {
                // If this isn't a key this package cares about,
                // then call the base class implementation
                base.OnLoadOptions(key, stream);
            }
        }

        protected override void OnSaveOptions(string key, Stream stream) {
            // if key is a string this package cares about then
            // stream can be used to write the data
            if (key == MyOptionKey) {
                try {
                    BinaryWriter writer = new BinaryWriter(stream);
                    // write the options here...
                    writer.Flush();
                    stream.Flush();
                } catch (Exception) {
                    // no options, ok, no problem.
                }
            } else {
                // if this isn't a key this package cares about,
                // then call the base class implementation
                base.OnSaveOptions(key, stream);
            }
        }              

Allen

Published Monday, July 12, 2004 12:28 PM by AllenD
Filed under:

Comments

No Comments
Anonymous comments are disabled

This Blog

Syndication

News

> These postings are provided "as-is" and confer no rights or warranties.


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker