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); }
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); }
private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if (string.IsNullOrEmpty(strFilePath)) { return(null); } AppConfigEx tConfig = null; XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); if (xdEnforced == null) { try { using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { tConfig = (AppConfigEx)xmlSerial.Deserialize(fs); } } catch (Exception) { } // Do not assert } else // Enforced configuration { try { 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 msAsm = new MemoryStream()) { xd.Save(msAsm); using (MemoryStream msRead = new MemoryStream( msAsm.ToArray(), false)) { tConfig = (AppConfigEx)xmlSerial.Deserialize(msRead); } } } catch (FileNotFoundException) { } catch (Exception) { Debug.Assert(false); } } if (tConfig != null) { tConfig.OnLoad(); } return(tConfig); }
private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if (string.IsNullOrEmpty(strFilePath)) { return(null); } AppConfigEx tConfig = null; XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); if (xdEnforced == null) { FileStream fs = null; try { fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); tConfig = (AppConfigEx)xmlSerial.Deserialize(fs); } catch (Exception) { } // Do not assert if (fs != null) { fs.Close(); fs = null; } } else // Enforced configuration { try { XmlDocument xd = new XmlDocument(); xd.Load(strFilePath); XmlUtil.MergeNodes(xd, xd.DocumentElement, xdEnforced.DocumentElement); MemoryStream msAsm = new MemoryStream(); xd.Save(msAsm); MemoryStream msRead = new MemoryStream(msAsm.ToArray(), false); tConfig = (AppConfigEx)xmlSerial.Deserialize(msRead); msRead.Close(); msAsm.Close(); } catch (FileNotFoundException) { } catch (Exception) { Debug.Assert(false); } } if (tConfig != null) { tConfig.OnLoad(); } return(tConfig); }
public static AppConfigEx Load() { AppConfigSerializer.GetConfigPaths(); // AppConfigEx cfgEnf = LoadConfigFileEx(m_strEnforcedConfigFile); // if(cfgEnf != null) // { // cfgEnf.Meta.IsEnforcedConfiguration = true; // return cfgEnf; // } XmlDocument xdEnforced = LoadEnforcedConfigFile(); AppConfigEx cfgGlobal = LoadConfigFileEx(m_strGlobalConfigFile, xdEnforced); AppConfigEx cfgUser = LoadConfigFileEx(m_strUserConfigFile, xdEnforced); if ((cfgGlobal == null) && (cfgUser == null)) { if (xdEnforced != null) { XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); try { using (MemoryStream msEnf = new MemoryStream()) { xdEnforced.Save(msEnf); using (MemoryStream msRead = new MemoryStream( msEnf.ToArray(), false)) { AppConfigEx cfgEnf = (AppConfigEx)xmlSerial.Deserialize(msRead); cfgEnf.OnLoad(); return(cfgEnf); } } } catch (Exception) { Debug.Assert(false); } } AppConfigEx cfgNew = new AppConfigEx(); cfgNew.OnLoad(); // Create defaults return(cfgNew); } else if ((cfgGlobal != null) && (cfgUser == null)) { return(cfgGlobal); } else if ((cfgGlobal == null) && (cfgUser != null)) { return(cfgUser); } cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration; return(cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal); }
public static AppConfigEx Load() { AppConfigSerializer.GetConfigPaths(); // AppConfigEx cfgEnf = LoadConfigFileEx(m_strEnforcedConfigFile); // if(cfgEnf != null) // { // cfgEnf.Meta.IsEnforcedConfiguration = true; // return cfgEnf; // } XmlDocument xdEnforced = LoadEnforcedConfigFile(); AppConfigEx cfgGlobal = LoadConfigFileEx(m_strGlobalConfigFile, xdEnforced); AppConfigEx cfgUser = LoadConfigFileEx(m_strUserConfigFile, xdEnforced); if((cfgGlobal == null) && (cfgUser == null)) { if(xdEnforced != null) { XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); try { MemoryStream msEnf = new MemoryStream(); xdEnforced.Save(msEnf); MemoryStream msRead = new MemoryStream(msEnf.ToArray(), false); AppConfigEx cfgEnf = (AppConfigEx)xmlSerial.Deserialize(msRead); cfgEnf.OnLoad(); msRead.Close(); msEnf.Close(); return cfgEnf; } catch(Exception) { Debug.Assert(false); } } AppConfigEx cfgNew = new AppConfigEx(); cfgNew.OnLoad(); // Create defaults return cfgNew; } else if((cfgGlobal != null) && (cfgUser == null)) return cfgGlobal; else if((cfgGlobal == null) && (cfgUser != null)) return cfgUser; cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration; return (cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal); }