/// <summary> /// 添加或更新配置项:如果不存在,则添加到configType4NewAdded指定类型的位置。 /// </summary> /// <param name="key">配置项对应的Key键。</param> /// <param name="configValue">配置项的新值。</param> /// <param name="configType4NewAdded">新增配置项的类型。</param> /// <returns>更新结果。</returns> public ConfigOperationResult AddOrUpdateConfig(string key, string configValue, AppConfigType configType4NewAdded) { if (this.collection.ContainsKey(key)) { return(UpdateConfig(key, configValue)); } else { AppConfigData tmpAppData = new AppConfigData() { ConfigType = configType4NewAdded, Key = key, Value = configValue, }; if (configType4NewAdded.IsRegistryConfig()) { ////RegEditHelp.WriteRegedit(key, configValue); throw new NotSupportedException("暂不支持注册表配置。"); } else { Configuration config = GetConfigurationInstance(configType4NewAdded); config.AppSettings.Settings.Add(key, configValue); RefreshConfigurationFile(config); } this.collection.Add(key, tmpAppData); } return(ConfigOperationResult.Failed); }
/// <summary> /// 从指定目录的文件,加载指定类型的配置项。 /// </summary> /// <param name="path"></param> /// <param name="filename"></param> /// <param name="configType">加载的配置项的类型。</param> private void LoadConfigsFromFile(string path, string filename, AppConfigType configType) { string baseDirectory = path; if (string.IsNullOrEmpty(path)) { baseDirectory = AppDomain.CurrentDomain.BaseDirectory; } string path2 = Path.Combine(path, filename); if (!File.Exists(path2)) { return; } ExeConfigurationFileMap file = new ExeConfigurationFileMap(); file.ExeConfigFilename = path2; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); if (config.AppSettings.Settings == null) { return; } foreach (var tmpKey in config.AppSettings.Settings.AllKeys) { this.AddConfig2Buffer(tmpKey, config.AppSettings.Settings[tmpKey].Value, configType); } }
private static bool IsNeededAppConfigType(Enum configType, AppConfigType neededConfigType) { if (configType.GetType() != typeof(AppConfigType)) { return(false); } AppConfigType tmpConfigType = (AppConfigType)configType; return(tmpConfigType == neededConfigType); }
/// <summary> /// 将指定键-值配置对添加到缓存。 /// </summary> /// <param name="key"></param> /// <param name="configValue"></param> /// <param name="configType"></param> private void AddConfig2Buffer(string key, string configValue, AppConfigType configType) { if (this.collection.ContainsKey(key)) { var conflictConfig = this.collection[key]; //LoggingService.Info(string.Format("更新冲突的配置项:{0}-{1}-{2}-->{3}-{4}-{5}。", // conflictConfig.ConfigType, conflictConfig.Key, // conflictConfig.Value, configType, key, configValue)); if (conflictConfig.ConfigType != configType) { var oldConfig = new AppConfigData() { ConfigType = conflictConfig.ConfigType, Key = conflictConfig.Key, Value = conflictConfig.Value, }; conflictConfig.SetConfigType(configType); } conflictConfig.SetValue(configValue); return; } if (string.IsNullOrEmpty(configValue)) { this.collection.Add(key, new AppConfigData() { ConfigType = configType, Key = key, Value = AppConfigData.NullValue, }); } else { this.collection.Add(key, new AppConfigData() { ConfigType = configType, Key = key, Value = configValue, }); } }
/// <summary> /// 获取指定配置文件的操作类。 /// </summary> /// <param name="configType"></param> /// <returns></returns> private Configuration GetConfigurationInstance(AppConfigType configType) { string filename = string.Empty; if (configType.IsApplicationConfig()) { filename = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, AppConfigFileName); } else { filename = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, UserConfigFileName); } ExeConfigurationFileMap file = new ExeConfigurationFileMap(); file.ExeConfigFilename = filename; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None); return(config); }
public static AppConfiguration GetAppConfig(AppConfigType type = AppConfigType.Default) => new AppConfiguration(_appIds[type]) { BaseUri = _baseUri ?? new Uri("http://localhost:12345"), MetadataPersistenceMode = MetadataPersistenceMode.NotEncrypted, };
private void initConfigValue() { if (DataSource.AppConfigID < 1) { return; } if (DataSource.StoreId == 0 && TargetStoreId > 0) { litStoreName.Text = Store.GetStoreName(TargetStoreId); pnlInheritWarning.Visible = true; } else { pnlInheritWarning.Visible = false; } AppConfigId.Value = DataSource.AppConfigID.ToString(); AppConfigType type = AppConfig.ParseTypeFromString(DataSource.ValueType); phValidators.Controls.Clear(); tbxValue.Visible = ddValue.Visible = cblValue.Visible = rblValue.Visible = false; switch (type) { case AppConfigType.@string: tbxValue.Visible = true; tbxValue.Text = DataSource.ConfigValue; break; case AppConfigType.boolean: rblValue.Visible = true; rblValue.SelectedValue = DataSource.ConfigValue.ToBool().ToString().ToLower(); pnlInheritWarning.Visible = false; break; case AppConfigType.integer: tbxValue.Visible = true; tbxValue.Text = DataSource.ConfigValue; RegularExpressionValidator regExInt = new RegularExpressionValidator(); regExInt.ValidationExpression = @"^-{0,1}\d+$"; regExInt.ID = DB.GetNewGUID(); regExInt.ControlToValidate = tbxValue.ID; phValidators.Controls.Add(regExInt); break; case AppConfigType.@decimal: case AppConfigType.@double: tbxValue.Visible = true; tbxValue.Text = DataSource.ConfigValue; FilteredTextBoxExtender extFilterNumeric = new FilteredTextBoxExtender(); extFilterNumeric.ID = DB.GetNewGUID(); extFilterNumeric.TargetControlID = tbxValue.ID; extFilterNumeric.FilterType = FilterTypes.Numbers | FilterTypes.Custom; extFilterNumeric.ValidChars = "."; phValidators.Controls.Add(extFilterNumeric); break; case AppConfigType.@enum: ddValue.Visible = true; ddValue.Items.Clear(); foreach (string value in DataSource.AllowableValues) { ddValue.Items.Add(value); } if (ddValue.Items.FindByValue(DataSource.ConfigValue) != null) { ddValue.SelectedValue = DataSource.ConfigValue; } break; case AppConfigType.multiselect: cblValue.Visible = true; cblValue.Items.Clear(); Func <string, bool> isIncluded = (optionValue) => { return(DataSource.ConfigValue.Split(',').Any(val => val.Trim().EqualsIgnoreCase(optionValue))); }; foreach (string optionValue in DataSource.AllowableValues) { ListItem option = new ListItem(optionValue, optionValue); option.Selected = isIncluded(optionValue); cblValue.Items.Add(option); } List <string> selectedValues = new List <string>(); foreach (ListItem option in cblValue.Items) { if (option.Selected) { selectedValues.Add(option.Value); } } break; case AppConfigType.invoke: ddValue.Visible = true; ddValue.Items.Clear(); bool invocationSuccessful = false; string errorMessage = string.Empty; try { if (DataSource.AllowableValues.Count == 3) { // format should be // {FullyQualifiedName},MethodName // Fully qualified name format is Namespace.TypeName,AssemblyName string typeName = DataSource.AllowableValues[0]; string assemblyName = DataSource.AllowableValues[1]; string methodName = DataSource.AllowableValues[2]; string fqName = typeName + "," + assemblyName; Type t = Type.GetType(fqName); object o = Activator.CreateInstance(t); MethodInfo method = o.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); object result = method.Invoke(o, new object[] { }); if (result is IEnumerable) { ddValue.DataSource = result; ddValue.DataBind(); invocationSuccessful = true; if (ddValue.Items.FindByValue(DataSource.ConfigValue) != null) { ddValue.SelectedValue = DataSource.ConfigValue; } } else { errorMessage = "Invocation method must return IEnumerable"; invocationSuccessful = false; } } else { errorMessage = "Invalid invocation value"; invocationSuccessful = false; } } catch (Exception ex) { errorMessage = ex.Message; invocationSuccessful = false; } if (invocationSuccessful == false) { ddValue.Items.Add(errorMessage); } break; default: break; } }
/// <summary> /// 设置配置项的类型。 /// </summary> /// <param name="configType">配置项类型。</param> public void SetConfigType(AppConfigType configType) { this.ConfigType = configType; }