Ejemplo n.º 1
0
        /*
         * public static Form StartGui(IConfig config, ViewerProxy form, Func<Form> createForm) {
         *  if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) {
         *      Form f = createForm();
         *      StartProxyClient(config, form, f);
         *      f.ShowDialog();
         *      return f;
         *  } else {
         *      object createLock = new object();
         *      Form f = null;
         *      bool started = false;
         *      Thread t = new Thread(() => {
         *          f = createForm();
         *          lock (createLock)
         *              MonitorChanged.PulseAll(createLock);
         *          started = true;
         *          StartProxyClient(config, form, f);
         *          f.ShowDialog();
         *      });
         *      t.SetApartmentState(ApartmentState.STA);
         *      t.Begin();
         *      if (!started)
         *          lock (createLock)
         *              MonitorChanged.Wait(createLock);
         *      return f;
         *  }
         * }
         *
         * private static void StartProxyClient(IConfig config, ViewerProxy form, Form f) {
         *  f.VisibleChanged += (source, args) => {
         *      if (!f.Visible)
         *          return;
         *      bool autostartProxy = Get(config, "AutoStartProxy", false);
         *      bool autostartClient = Get(config, "AutoStartClient", false);
         *
         *      if (autostartProxy || autostartClient)
         *          while (!form.StartProxy())
         *              form.ProxyConfig.ProxyPort++;
         *      if (autostartClient)
         *          form.StartClient();
         *  };
         * }
         */

        public static IConfigSource AddFile(IConfigSource config, string file)
        {
            if (File.Exists(file) && Path.GetExtension(file).ToUpper().Equals(".CONFIG"))
            {
                try {
                    DotNetConfigSource dotnet = new DotNetConfigSource(file);
                    //config.Merge(dotnet);
                    dotnet.Merge(config);
                    return(dotnet);
                } catch (Exception e) {
                    Logger.Warn("Unable to load app configuration file " + file + "'." + e.Message + ".\n" + e.StackTrace);
                }
            }
            else if (File.Exists(file) && Path.GetExtension(file).ToUpper().Equals(".INI"))
            {
                try {
                    IniDocument     doc = new IniDocument(file, IniFileType.WindowsStyle);
                    IniConfigSource ini = new IniConfigSource(doc);
                    //config.Merge(ini);
                    ini.Merge(config);
                    return(ini);
                } catch (Exception e) {
                    Logger.Warn("Unable to load ini configuration file " + file + "'." + e.Message + ".\n" + e.StackTrace);
                }
            }
            return(config);
        }
Ejemplo n.º 2
0
        public void MergeAndSave()
        {
            string xmlFileName = "NiniConfig.xml";

            XmlDocument doc = NiniDoc();

            AddSection(doc, "Pets");
            AddKey(doc, "Pets", "cat", "Muffy");
            AddKey(doc, "Pets", "dog", "Rover");
            AddKey(doc, "Pets", "bird", "Tweety");
            doc.Save(xmlFileName);

            StringWriter writer = new StringWriter();

            writer.WriteLine("[Pets]");
            writer.WriteLine("cat = Becky");              // overwrite
            writer.WriteLine("lizard = Saurus");          // new
            writer.WriteLine("[People]");
            writer.WriteLine(" woman = Jane");
            writer.WriteLine(" man = John");
            IniConfigSource iniSource = new IniConfigSource
                                            (new StringReader(writer.ToString()));

            DotNetConfigSource xmlSource = new DotNetConfigSource(xmlFileName);

            xmlSource.Merge(iniSource);

            IConfig config = xmlSource.Configs["Pets"];

            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Jane", config.Get("woman"));
            Assert.AreEqual("John", config.Get("man"));

            config.Set("woman", "Tara");
            config.Set("man", "Quentin");

            xmlSource.Save();

            xmlSource = new DotNetConfigSource(xmlFileName);

            config = xmlSource.Configs["Pets"];
            Assert.AreEqual(4, config.GetKeys().Length);
            Assert.AreEqual("Becky", config.Get("cat"));
            Assert.AreEqual("Rover", config.Get("dog"));
            Assert.AreEqual("Saurus", config.Get("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual(2, config.GetKeys().Length);
            Assert.AreEqual("Tara", config.Get("woman"));
            Assert.AreEqual("Quentin", config.Get("man"));

            File.Delete(xmlFileName);
        }