/[RomCheater]/trunk/RomCheater.UserSettingsSupport/SettingsSubscriberBase.cs
ViewVC logotype

Annotation of /trunk/RomCheater.UserSettingsSupport/SettingsSubscriberBase.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations) (download)
Wed May 9 11:29:33 2012 UTC (9 years, 6 months ago) by william
File size: 2021 byte(s)
+ add settings upgrade subscriber support (needs to be called in a static constructor for types that are always used)

1 william 34 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Reflection;
6     using System.Configuration;
7    
8     namespace RomCheater.UserSettingsSupport
9     {
10     public sealed class SettingSubscriber
11     {
12     private class SettingsEventArgs : EventArgs
13     {
14     public SettingsEventArgs() :this(null) { }
15     public SettingsEventArgs(System.Configuration.ApplicationSettingsBase settings) { this.Settings = settings; }
16     public System.Configuration.ApplicationSettingsBase Settings { get; private set; }
17     }
18     private static EventHandler<SettingsEventArgs> OnSubscribeAddedEvent;
19     static SettingSubscriber() { OnSubscribeAddedEvent += new EventHandler<SettingsEventArgs>(OnSubscribeAdded); }
20     public static void AddSubscriber(System.Configuration.ApplicationSettingsBase settings) { if (OnSubscribeAddedEvent != null) { OnSubscribeAddedEvent(new SettingSubscriber(), new SettingsEventArgs(settings)); } }
21     private static void OnSubscribeAdded(object SubscribingAssembly, SettingsEventArgs e)
22     {
23     if (e.Settings != null)
24     {
25     foreach (SettingsProperty prop in e.Settings.Properties)
26     {
27     if (prop.Name == "UpgradeRequired")
28     {
29     bool UpgradeRequired = Convert.ToBoolean(e.Settings[prop.Name]);
30     try
31     {
32     if (UpgradeRequired)
33     {
34     e.Settings.Upgrade();
35     e.Settings.Reload();
36     e.Settings[prop.Name] = false;
37     e.Settings.Save();
38     }
39     }
40     catch { e.Settings.Reset(); }
41     break;
42     }
43     }
44     }
45     }
46     }
47     }

  ViewVC Help
Powered by ViewVC 1.1.22