//讀檔並存入Dictionary private static void ReadConfigFromFile() { string[] configString = File.ReadAllLines(userConfigPath); //每一行 foreach (string line in configString) { ConfigKey eachKey = (ConfigKey)Enum.Parse(typeof(ConfigKey), line.Split(':')[0]); string valueString = line.Split(':')[1]; bool boolValue; int intValue; //Bool <有例外> if (Boolean.TryParse(valueString, out boolValue)) { SetConfig(eachKey, boolValue); Console.WriteLine("[Set Bool]" + eachKey.ToString() + ":" + boolValue.ToString()); } //Int else if (int.TryParse(valueString, out intValue)) { SetConfig(eachKey, intValue); Console.WriteLine("[Set Int]" + eachKey.ToString() + ":" + intValue.ToString()); } else { MessageBox.Show("Fatal error:無法解析 Config"); //刪除config並關閉程式 <待寫> } } }
public static void Check(ConfigKey key, string currentVal) { if (!string.IsNullOrEmpty(currentVal) && key.ToString().StartsWith("Filepath", StringComparison.Ordinal) && !key.ToString().EndsWith("Dir", StringComparison.Ordinal) && !key.ToString().Contains("Checksum")) { var otherKeyName = key.ToString().Replace("Filepath", "FilepathChecksum"); var otherKey = Enum.Parse(typeof(ConfigKey), otherKeyName); VerifyChecksum((ConfigKey)otherKey, currentVal); } }
public static string GetConfig(ConfigKey key, string defaultValue = "") { if (!File.Exists(ConfigFile)) { return(defaultValue); } return(IniUtils.Read(ConfigFile, CONFIGKEY, key.ToString(), defaultValue).Trim()); }
private string GetAppSettings(ConfigKey key) { var errorMessage = string.Format(MissingAppSetting, key.ToString()); try { var value = ConfigurationManager.AppSettings[key.ToString()]; if (value == null) { throw new ConfigurationErrorsException(errorMessage); } return(ConfigurationManager.AppSettings[key.ToString()]); } catch (Exception ex) { throw new ConfigurationErrorsException(errorMessage, ex); } }
private static void SetIntValue(ConfigKey key, int value) { RegistryKey regKey = Registry.CurrentUser.OpenSubKey(RegKeyName, true); if (regKey != null) { regKey.SetValue(key.ToString(), value); } }
internal static T GetPersistentConfig <T>(ConfigKey key, T defaultValue) { object value = StorageHelper.LoadConfig(key.ToString()); if (value != null) { return((T)value); } return(defaultValue); }
public static void SetConfig(ConfigKey key, string value) { if (!File.Exists(ConfigFile)) { using (var fs = File.Create(ConfigFile)) { fs.Close(); } } IniUtils.Write(ConfigFile, CONFIGKEY, key.ToString(), value); }
/// <summary> /// 返回对应键的值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key">键</param> /// <param name="def">默认值</param> /// <returns></returns> public static T GetVal <T>(ConfigKey key, T def = default(T)) { try { def = (T)Convert.ChangeType(Configuration.GetSection(key.ToString()).Value, typeof(T)); } catch (Exception e) { Debug.WriteLine(e.Message); } return(def); }
private string GetConnectionString(ConfigKey key) { var errorMessage = string.Format(MissingConnectionString, key); try { return(ConfigurationManager.ConnectionStrings[key.ToString()].ConnectionString); } catch (Exception ex) { _logger.Error(errorMessage + ex); throw new ConfigurationErrorsException(errorMessage); } }
int GetAppSettingValueAsInt(ConfigKey key) { return(settingsReader.GetAppSettingValueAsInt(key.ToString())); }
internal static void SetPersistentConfig <T>(ConfigKey key, T value) { StorageHelper.SaveConfig(key.ToString(), value); }
public T GetConfiguration <T>(ConfigKey key) where T : class { var target = _ConfigurationItems.FirstOrDefault(c => c.Key == key.ToString()); return(target?.GetConfigValue <T>()); }
/// <summary> /// Gets the setting from the Web.Config or App.Config /// </summary> /// <param name="webConfigEnum">The key, as a ConfigKey enum</param> /// <returns>String value or empty string</returns> public static string GetSetting(ConfigKey webConfigEnum) { string key = webConfigEnum.ToString(); //Use for *required* config keys; Nick wants to throw an exception if it's missing. var value = ConfigurationManager.AppSettings[webConfigEnum.ToString()]; if (value == null) throw new Exception("Missing config key: " + key); return value.ToString(); }
public string Read(ConfigKey key) { return ConfigurationManager.AppSettings[key.ToString()]; }
private static bool GetBoolValue(ConfigKey key) { RegistryKey regKey = Registry.CurrentUser.OpenSubKey(RegKeyName); return(regKey != null && ((int)regKey.GetValue(key.ToString()) != 0)); }
public static object GetAppSetting(ConfigKey key) { return(ConfigurationManager.AppSettings[key.ToString()]); }
public string GetSetting(ConfigKey configKey) { return ConfigurationManager.AppSettings[configKey.ToString()]; }
List <string> GetAppSettingValuesAsStringList(ConfigKey key) { return(settingsReader.GetAppSettingValueAsSeparatedStringList(key.ToString(), ',')); }
string GetConnectionString(ConfigKey key) { return(settingsReader.GetConnectionString(key.ToString())); }
private static int GetIntValue(ConfigKey key) { RegistryKey regKey = Registry.CurrentUser.OpenSubKey(RegKeyName); return(regKey != null ? (int)regKey.GetValue(key.ToString()) : 0); }