コード例 #1
0
        private bool CanForcePersist(string appSettingsFilePath)
        {
            if (appSettingsFilePath.IsNullOrWhiteSpace())
            {
                return(false);
            }

            appSettingsFilePath = ChoConfigurationManager.GetFullPath(appSettingsFilePath);
            if (!File.Exists(appSettingsFilePath))
            {
                return(true);
            }

            //try
            //{
            //    XDocument doc = XDocument.Load(appSettingsFilePath);
            //    if (doc.Root == null
            //    || doc.Root.Name.LocalName != APP_SETTINGS_SECTION_NAME)
            //        return true;
            //}
            //catch
            //{
            //    return true;
            //}

            return(false);
        }
コード例 #2
0
ファイル: ChoConfigStorage.cs プロジェクト: commodus/Cinchoo
        public override object Load(ChoBaseConfigurationElement configElement, XmlNode node)
        {
            base.Load(configElement, node);

            //if (configElement.ConfigFilePath.IsNullOrWhiteSpace())
            //    UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);
            //else
            //    UnderlyingConfigFilePath = configElement.ConfigFilePath;

            UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);
            if (UnderlyingConfigFilePath.IsNullOrWhiteSpace())
            {
                configElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                UnderlyingConfigFilePath = configElement.ConfigFilePath;
            }

            ConfigElement[UNDERLYING_CONFIG_PATH] = UnderlyingConfigFilePath;

            ConfigFilePath = GetFullPath(UnderlyingConfigFilePath);

            if (IsAppConfigFile)
            {
                if (!configElement.ConfigFilePath.IsNullOrWhiteSpace())
                {
                    UnderlyingConfigFilePath = configElement.ConfigFilePath;
                    ConfigFilePath           = GetFullPath(UnderlyingConfigFilePath);
                }
            }

            if (node != null && !IsAppConfigFile)
            {
                if (configElement.Persistable)
                {
                    ChoXmlDocument.CreateXmlFileIfEmpty(ConfigFilePath);
                }

                using (ChoXmlDocument document = new ChoXmlDocument(ConfigFilePath))
                {
                    string nodeName = configElement.ConfigElementPath;
                    IncludeFilePaths = document.IncludeFiles;
                    if (document.XmlDocument != null && document.XmlDocument.DocumentElement != null)
                    {
                        ChoConfiguration configuration = document.XmlDocument.DocumentElement.ToObject <ChoConfiguration>();
                        if (configuration != null)
                        {
                            ConfigNode = ChoXmlDocument.GetXmlNode(nodeName, configuration.RestOfXmlDocumentElements);
                        }
                    }
                }
            }

            return(null);
        }
コード例 #3
0
        public override object Load(ChoBaseConfigurationElement configElement, XmlNode node)
        {
            base.Load(configElement, node);

            ConfigurationManager.RefreshSection(APP_SETTINGS_SECTION_NAME);
            ChoConfigurationManager.Refresh();
            ConfigFilePath = ChoConfigurationManager.ApplicationConfigurationFilePath;
            string appSettingsFilePath = GetAppSettingsFilePathIfAnySpecified();

            configElement[ChoConfigurationConstants.FORCE_PERSIST] = CanForcePersist(appSettingsFilePath);

            ConfigSectionName = APP_SETTINGS_SECTION_NAME;
            RefreshSection(appSettingsFilePath);
            return(ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.ToNameValueCollection());
        }
コード例 #4
0
        internal static T GetSection <T>(string sectionName, T defaultValue = default(T))
            where T : ConfigurationSection
        {
            T instance = (T)ChoConfigurationManager.GetSection(sectionName);

            if (instance == null)
            {
                instance = defaultValue;
                if (!ChoAppFrxSettings.Me.DisableFrxConfig)
                {
                    instance.Save(sectionName);
                }
            }
            ChoApplication.RaiseAfterAppFrxSettingsLoaded(instance);
            return(instance);
        }
コード例 #5
0
ファイル: ChoConfigStorage.cs プロジェクト: commodus/Cinchoo
        protected virtual void PersistConfigSectionDefinition(XmlDocument xmlDocument, object data)
        {
            XmlNode configSectionsNode = xmlDocument.SelectSingleNode("configSections");

            if (configSectionsNode == null)
            {
                configSectionsNode = xmlDocument.MakeXPath("configSections");
            }

            if (configSectionsNode == null)
            {
                return;
            }

            XmlNode sectionNode = MakeSectionDefinition(xmlDocument, configSectionsNode, ConfigElement.ConfigElementPath, ConfigElement.ConfigSectionHandlerType.AssemblyQualifiedName);

            ChoConfigurationManager.OpenExeConfiguration(false);
        }
コード例 #6
0
        private void RefreshSection(string appSettingsFilePath)
        {
            if (!appSettingsFilePath.IsNullOrWhiteSpace())
            {
                if (ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File) != ChoConfigurationManager.GetFullPath(appSettingsFilePath))
                {
                    ConfigElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                }
            }

            if (!ChoConfigurationManager.ApplicationConfiguration.AppSettings.File.IsNullOrWhiteSpace())
            {
                if (!File.Exists(ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File)))
                {
                    ConfigElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                }
            }
        }
コード例 #7
0
        internal static object GetConfig(Type configObjectType, string configSectionPath, Type defaultConfigSectionHandlerType,
                                         ChoBaseConfigurationElement configElement)
        {
            if (_configuration == null)
            {
                return(null);
            }

            ChoConfigurationSection configurationSection = ChoConfigurationManager.GetConfigSection(configSectionPath);

            XmlNode[] restOfXmlElements = null;

            Type configSectionHandlerType = defaultConfigSectionHandlerType;

            if (configurationSection != null)
            {
                restOfXmlElements = configurationSection.RestOfXmlElements;
                if (!configurationSection.Type.IsNullOrWhiteSpace())
                {
                    configSectionHandlerType = ChoType.GetType(configurationSection.Type);
                }
            }

            configElement.ConfigSectionHandlerType = configSectionHandlerType;
            if (!typeof(IChoConfigurationSectionHandler).IsAssignableFrom(configSectionHandlerType))
            {
                throw new ChoConfigurationException("Configuration section handler type should be of IChoConfigurationSectionHandler type.");
            }

            if (typeof(IChoNullConfigurationSectionableHandler).IsAssignableFrom(configSectionHandlerType))
            {
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, null, restOfXmlElements, configElement));
            }
            else
            {
                XmlNode node = ChoXmlDocument.GetXmlNode(configSectionPath, _configuration.RestOfXmlDocumentElements);
                //if (node != null)
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, node, restOfXmlElements, configElement));
            }
            //return new ChoDefaultApplicationConfigSection(configObjectType);
        }
コード例 #8
0
        private string GetAppSettingsFilePathIfAnySpecified()
        {
            string filePath = null;

            System.Configuration.AppSettingsSection appSettings = ChoConfigurationManager.ApplicationConfiguration.AppSettings;
            if (!appSettings.File.IsNullOrEmpty())
            {
                filePath = appSettings.File.NTrim();
            }
            else
            {
                filePath = ConfigElement.ConfigFilePath.NTrim();
            }

            if (Directory.Exists(ChoConfigurationManager.GetFullPath(filePath)))
            {
                return(null);
            }
            else
            {
                return(filePath);
            }
        }
コード例 #9
0
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            if (!(data is NameValueCollection))
            {
                throw new ChoConfigurationException("Data object is not NameValueCollection object.");
            }
            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            bool   saveMainConfig      = false;
            string appSettingsFilePath = this.GetAppSettingsFilePathIfAnySpecified();

            if (!appSettingsFilePath.IsNullOrWhiteSpace())
            {
                if (ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File) != ChoConfigurationManager.GetFullPath(appSettingsFilePath))
                {
                    saveMainConfig = true;
                    ChoConfigurationManager.ApplicationConfiguration.AppSettings.File = appSettingsFilePath;
                }
            }

            NameValueCollection nameValueCollection = ((NameValueCollection)data);

            if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.File.IsNullOrWhiteSpace())
            {
                foreach (string key1 in nameValueCollection.Keys)
                {
                    if (ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.AllKeys.Contains(key1))
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings[key1].Value = nameValueCollection[key1];
                    }
                    else
                    {
                        ChoConfigurationManager.ApplicationConfiguration.AppSettings.Settings.Add(new KeyValueConfigurationElement(key1, nameValueCollection[key1]));
                    }
                }

                ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
            }
            else
            {
                if (saveMainConfig)
                {
                    ChoConfigurationManager.ApplicationConfiguration.Save(ConfigurationSaveMode.Modified);
                }
                File.WriteAllText(ChoConfigurationManager.GetFullPath(ChoConfigurationManager.ApplicationConfiguration.AppSettings.File), GetAppSettingsText(nameValueCollection));
            }
            //ConfigurationManager.RefreshSection(APP_SETTINGS_SECTION_NAME);
            ChoConfigurationManager.Refresh();
        }
コード例 #10
0
 public override void GetConfig(Type configObjectType, bool refresh)
 {
     ConfigSection = ChoConfigurationManager.GetConfig(configObjectType, ConfigElementPath, DefaultConfigSectionHandlerType, this) as ChoConfigSection;
 }
コード例 #11
0
        public override object Load(ChoBaseConfigurationElement configElement, XmlNode node)
        {
            base.Load(configElement, node);

            //if (configElement.ConfigFilePath.IsNullOrWhiteSpace())
            //    UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);
            //else
            //    UnderlyingConfigFilePath = configElement.ConfigFilePath;

            UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);

            if (UnderlyingConfigFilePath.IsNullOrWhiteSpace() ||
                UnderlyingConfigFilePath != configElement.ConfigFilePath)
            {
                if (!configElement.ConfigFilePath.IsNullOrWhiteSpace() && UnderlyingConfigFilePath.IsNullOrWhiteSpace())
                {
                    configElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                    UnderlyingConfigFilePath = configElement.ConfigFilePath;
                }
            }

            ConfigElement[UNDERLYING_CONFIG_PATH] = UnderlyingConfigFilePath;
            if (ChoConfigurationManager.IsSeperateConfigFileSpecified(node))
            {
                ConfigElement[PREV_UNDERLYING_CONFIG_PATH] = UnderlyingConfigFilePath;
            }

            ConfigFilePath = GetFullPath(UnderlyingConfigFilePath);

            if (IsAppConfigFile)
            {
                if (!configElement.ConfigFilePath.IsNullOrWhiteSpace())
                {
                    UnderlyingConfigFilePath = configElement.ConfigFilePath;
                    ConfigFilePath           = GetFullPath(UnderlyingConfigFilePath);

                    ConfigElement[UNDERLYING_CONFIG_PATH] = ConfigFilePath;
                }
            }

            if (/*node != null && */ !IsAppConfigFile)
            {
                if (!IsCustomFormatDataStorage)
                {
                    if (File.Exists(ConfigFilePath))
                    {
                        using (ChoXmlDocument document = new ChoXmlDocument(ConfigFilePath))
                        {
                            string nodeName = configElement.ConfigElementPath;
                            IncludeFilePaths = document.IncludeFiles;
                            if (document.XmlDocument != null && document.XmlDocument.DocumentElement != null)
                            {
                                ChoConfiguration configuration = document.XmlDocument.DocumentElement.ToObject <ChoConfiguration>();
                                if (configuration != null)
                                {
                                    ConfigNode = ChoXmlDocument.GetXmlNode(nodeName, configuration.RestOfXmlDocumentElements);
                                }
                            }
                        }
                    }
                }
                //if (configElement.Persistable)
                //    ChoXmlDocument.CreateXmlFileIfEmpty(ConfigFilePath);

                //using (ChoXmlDocument document = new ChoXmlDocument(ConfigFilePath))
                //{
                //    string nodeName = configElement.ConfigElementPath;
                //    IncludeFilePaths = document.IncludeFiles;
                //    if (document.XmlDocument != null && document.XmlDocument.DocumentElement != null)
                //    {
                //        ChoConfiguration configuration = document.XmlDocument.DocumentElement.ToObject<ChoConfiguration>();
                //        if (configuration != null)
                //        {
                //            ConfigNode = ChoXmlDocument.GetXmlNode(nodeName, configuration.RestOfXmlDocumentElements);
                //        }
                //    }
                //}
            }

            return(null);
        }