Exemplo n.º 1
0
        public static AppConfigEx Load()
        {
            GetConfigPaths();

            // AppConfigEx cfgEnf = LoadConfigFileEx(g_strEnforcedConfigFile);
            // if(cfgEnf != null)
            // {
            //	cfgEnf.Meta.IsEnforcedConfiguration = true;
            //	return cfgEnf;
            // }
            XmlDocument xdEnforced = LoadEnforcedConfigFile();

            AppConfigEx cfgGlobal = LoadConfigFileEx(g_strGlobalConfigFile, xdEnforced);

            if ((cfgGlobal != null) && !cfgGlobal.Meta.PreferUserConfiguration)
            {
                return(cfgGlobal);                // Do not show error for corrupted local configuration
            }
            AppConfigEx cfgUser = LoadConfigFileEx(g_strUserConfigFile, xdEnforced);

            if ((cfgGlobal == null) && (cfgUser == null))
            {
                if (xdEnforced != null)
                {
                    // Create an empty configuration file and merge it with
                    // the enforced configuration; this ensures that merge
                    // behaviors (like the node mode 'Remove') work as intended
                    try
                    {
                        string strFile = Program.TempFilesPool.GetTempFileName("xml");
                        File.WriteAllText(strFile, AppConfigEx.GetEmptyConfigXml(),
                                          StrUtil.Utf8);

                        AppConfigEx cfg = LoadConfigFileEx(strFile, xdEnforced);
                        if (cfg != null)
                        {
                            return(cfg);
                        }
                        Debug.Assert(false);
                    }
                    catch (Exception) { Debug.Assert(false); }
                }

                AppConfigEx cfgNew = new AppConfigEx();
                cfgNew.OnLoad();                 // Create defaults
                return(cfgNew);
            }
            if ((cfgGlobal != null) && (cfgUser == null))
            {
                return(cfgGlobal);
            }
            if ((cfgGlobal == null) && (cfgUser != null))
            {
                return(cfgUser);
            }

            cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration;
            return(cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal);
        }
Exemplo n.º 2
0
        private static AppConfigEx LoadConfigFileEx(string strFilePath,
                                                    XmlDocument xdEnforced)
        {
            if (string.IsNullOrEmpty(strFilePath))
            {
                Debug.Assert(false); return(null);
            }

            AppConfigEx tConfig = null;

            try
            {
                if (!File.Exists(strFilePath))
                {
                    return(null);
                }

                XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx));

                if (xdEnforced == null)
                {
                    using (FileStream fs = new FileStream(strFilePath,
                                                          FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        tConfig = (AppConfigEx)xs.Deserialize(fs);
                    }
                }
                else                 // Enforced configuration
                {
                    XmlDocument xd = XmlUtilEx.CreateXmlDocument();
                    xd.Load(strFilePath);

                    XmContext ctx = new XmContext(xd, AppConfigEx.GetNodeOptions,
                                                  AppConfigEx.GetNodeKey);
                    XmlUtil.MergeElements(xd.DocumentElement, xdEnforced.DocumentElement,
                                          "/" + xd.DocumentElement.Name, null, ctx);

                    using (MemoryStream msW = new MemoryStream())
                    {
                        xd.Save(msW);

                        using (MemoryStream msR = new MemoryStream(msW.ToArray(), false))
                        {
                            tConfig = (AppConfigEx)xs.Deserialize(msR);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FileDialogsEx.ShowConfigError(strFilePath, ex.Message, false, true);
            }

            if (tConfig != null)
            {
                tConfig.OnLoad();
            }
            return(tConfig);
        }
Exemplo n.º 3
0
        public static bool Save(AppConfigEx tConfig)
        {
            if (tConfig == null)
            {
                Debug.Assert(false); return(false);
            }
            if (!tConfig.Application.ConfigSave)
            {
                return(false);
            }

            GetConfigPaths();

            if (tConfig.Meta.PreferUserConfiguration)
            {
                EnsureDirOfFileExists(g_strUserConfigFile);
                if (SaveConfigFileEx(tConfig, g_strUserConfigFile))
                {
                    return(true);
                }

                if (SaveConfigFileEx(tConfig, g_strGlobalConfigFile))
                {
                    return(true);
                }
            }
            else             // Prefer global
            {
                if (SaveConfigFileEx(tConfig, g_strGlobalConfigFile))
                {
                    return(true);
                }

                EnsureDirOfFileExists(g_strUserConfigFile);
                if (SaveConfigFileEx(tConfig, g_strUserConfigFile))
                {
                    return(true);
                }
            }

            return(false);
        }