Storing User Configurations in .Net 1.1

 .Net 2.0 will introduce My.Settings to store user preferences. For .Net 1.1 i wanted to use approach described by Rockford Lhotka in MSDN article “Storing User Configurations”. It requires to create derived class with properties that you want to store.

I found 2 tips when created the derived class that is using IsolatedStorage.

1. Ignore FileNotFoundException error when load storage the first time.
2. Use assignment with cast when Load class from storage, e.g.
 
 MySettings=MySettings.Load();

See the sample derived class below:

      public class MySettings: vbUserSettings.UserSettingsBase

      {//based on http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet07082003.asp

            //in .Net 2.0 will be replaced with My.Settings  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vbmysettings.asp 

            //Constructors

            public MySettings(): base()//required for serialization

            {}

            public MySettings(vbUserSettings.UserStorageOption StorageOption): base(StorageOption)

            {}

           

            /// <summary>

            /// Loads casted object

            /// </summary>

            /// <returns></returns>

            /// <remarks> IMPORTANT: return value of the function should be used</remarks>

            /// <example>

            ///         MySettings=MySettings.Load();

            ///     MySettings.Load();// Incorrect!!, the cast is not applied and derived class properties are not assigned

            /// </example>

            public  MySettings Load()

            {

                  try

                  {

                        return (MySettings)Restore();

                  }

                  catch (vbUserSettings.UserSettingsException  exc) //System.IO.FileNotFoundException

                  {

                        Debug.WriteLine(exc.ToString());

                        //new file, ignore the error

                  }

                  return this;     

            }

            //Derived class properties and fields

            public ImportModeEnum importMode;

      }

 

posted @ Tuesday, October 25, 2005 8:56 AM
Print

Comments on this entry:

# re: Storing User Configurations in .Net 1.1

Left by kedar at 7/25/2006 8:06 PM
Gravatar
gr8 but an example is really appreciated

# re: Storing User Configurations in .Net 1.1

Left by web development company at 8/20/2009 10:36 AM
Gravatar
Interesting,

is it the same with .net 3.5?

Thanks for writing, most people don't bother.

# re: Storing User Configurations in .Net 1.1

Left by Michael Freidgeim at 8/21/2009 6:41 AM
Gravatar
In 2.0 and greater User Settings support is coming "out of the box" for WinForms

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789