public static SimpleConfig LoadConfigRegistry(string appKeyIn, SimpleConfigRegistryNode regNodeIn, SimpleConfigRegistryHive hiveIn)
        {
            SimpleConfig optionsOut = new SimpleConfig();

            RegistryView regView = RegistryView.Default;

            switch (regNodeIn)
            {
            case SimpleConfigRegistryNode.NODE_32:
                regView = RegistryView.Registry32;
                break;

            case SimpleConfigRegistryNode.NODE_64:
                regView = RegistryView.Registry64;
                break;
            }

            RegistryHive regHive = RegistryHive.LocalMachine;

            if (SimpleConfigRegistryHive.HIVE_LOCAL_USER == hiveIn)
            {
                regHive = RegistryHive.CurrentUser;
            }

            try {
                using (RegistryKey baseKey = RegistryKey.OpenBaseKey(regHive, regView)) {
                    using (RegistryKey loadKey = baseKey.OpenSubKey("Software\\" + appKeyIn)) {
                        if (null == loadKey)
                        {
                            // The key doesn't exist, so we can't do anything, anyway.
                            return(new SimpleConfig());
                        }

                        foreach (string valueName in loadKey.GetValueNames())
                        {
                            optionsOut.Set(
                                valueName,
                                (string)loadKey.GetValue(
                                    valueName,
                                    ""
                                    )
                                );
                            if (null == optionsOut.Get(valueName, null))
                            {
                                optionsOut.Set(valueName, "");
                            }
                        }
                    }
                }
            } catch (UnauthorizedAccessException ex) {
                // Wrap and throw upwards to isolate usings.
                throw new SimpleConfigException(ex.Message);
            }

            return(optionsOut);
        }
        public static SimpleNotifyMailer.Options GetNotifyOptionsRegistry(string appKeyIn)
        {
            SimpleNotifyMailer.Options optionsOut = new SimpleNotifyMailer.Options();
            SimpleConfig appConfig = SimpleConfig.LoadConfigRegistry(appKeyIn);

            optionsOut.Server      = appConfig.Get("NotifyMailServer", "127.0.0.1");
            optionsOut.Port        = appConfig.Get("NotifyMailPort", "25");
            optionsOut.FromAddress = appConfig.Get("NotifyMailFromAddress", "*****@*****.**");
            optionsOut.ToAddresses = appConfig.GetList("NotifyMailAddresses", ',');

            return(optionsOut);
        }
        public static void SetNotifyOptionsRegistry(string appKeyIn, string serverIn, string portIn, string fromAddressIn, string[] toAddressesIn)
        {
            SimpleConfig appConfig = new SimpleConfig();

            appConfig.Set("NotifyMailAddresses", string.Join(",", toAddressesIn));
            appConfig.Set("NotifyMailServer", serverIn);
            appConfig.Set("NotifyMailFromAddress", fromAddressIn);
            try {
                appConfig.Set("NotifyMailPort", int.Parse(portIn).ToString());
            } catch (FormatException ex) {
                throw new SimpleNotifyMailerException(ex.Message);
            }

            appConfig.SaveConfigRegistry(appKeyIn);
        }
Ejemplo n.º 4
0
        public void TestSimpleConfigUserSaveLoadRegistry()
        {
            string appKey       = "SimpleConfigTests";
            string testKey1     = "Test";
            string testVal1     = "Bears";
            string testKey2     = "TestArray";
            string testVal2     = "Bears,Birds,Barns";
            string testVal2Sub2 = "Barns";

            SimpleConfig config = new SimpleConfig();

            config.Set(testKey1, testVal1);
            config.Set(testKey2, testVal2);
            config.SaveConfigRegistry(appKey, SimpleConfigRegistryNode.NODE_DEFAULT, SimpleConfigRegistryHive.HIVE_LOCAL_USER);
            Assert.AreEqual(config.Get(testKey1, ""), testVal1);
            Assert.AreEqual(config.GetList(testKey2, ',')[2], testVal2Sub2);

            SimpleConfig testConfig = SimpleConfig.LoadConfigRegistry(appKey, SimpleConfigRegistryNode.NODE_DEFAULT, SimpleConfigRegistryHive.HIVE_LOCAL_USER);

            Assert.AreEqual(testConfig.Get(testKey1, ""), testVal1);
            Assert.AreEqual(testConfig.GetList(testKey2, ',')[2], testVal2Sub2);
        }
 public static SimpleConfig LoadConfigRegistry(string appKeyIn, SimpleConfigRegistryNode regNodeIn)
 {
     return(SimpleConfig.LoadConfigRegistry(appKeyIn, regNodeIn, SimpleConfigRegistryHive.HIVE_LOCAL_MACHINE));
 }
 public static SimpleConfig LoadConfigRegistry(string appKeyIn)
 {
     return(SimpleConfig.LoadConfigRegistry(appKeyIn, SimpleConfigRegistryNode.NODE_DEFAULT));
 }