/// <summary> /// string SetSetting(string key, string value) /// Set the setting passed to the passed value /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static string SetSetting(string key, string value) { String _value = ""; bool bolFound = false; try { if (configSettings.Count < 1) { // load the config file ConfigRoutines.ReadConfigFile(); } for (int n = 0; n < configSettings.Count; n++) { udtConfigSettings configSetting = configSettings[n]; if (configSetting.key.ToUpper().Equals(key.ToUpper())) { configSetting.value = value; configSettings[n] = configSetting; bolFound = true; break; } } // if not found, add to the list if (!bolFound) { udtConfigSettings configSetting = new udtConfigSettings(); configSetting.key = key; configSetting.value = value; configSetting.originalValue = value; configSetting.source = "D"; configSettings.Add(configSetting); } } catch (Exception ex) { CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".SetSetting(s,s) > " + ex.Message); } return _value; }
/// <summary> /// int ReadConfigFile() /// Read the config file in and load the configuration records /// </summary> /// <returns></returns> public static int ReadConfigFile() { String _fileName = ""; string _originalFileName = ""; int _numberOfLinesRead = 0; string _key = ""; string _value = ""; bool _userSettings = false; bool _setting = false; int _offset = 0; int _offset2 = 0; string _buffer = ""; List<udtConfigSettings> _configSettings = configSettings; try { dbConfigSettingsFound = false; // remove the exe name _offset = CommonRoutines.basePath.LastIndexOf("\\"); // get the logged on user string _userId = CommonRoutines.GetCurrentUser(); CSA.currentUser = CommonRoutines.GetCurrentUser(); // strip any prefix from the user if (_userId.IndexOf('\\') > 0) { int _prefixOffset = _userId.LastIndexOf('\\'); if (_prefixOffset >= 0) { _userId = _userId.Substring(_prefixOffset + 1); } } // get the IGenForms.config _fileName = CommonRoutines.GetEXEName() + ".config"; string _userFileName = (_userId != "")? _userId + "_" + _fileName:_fileName; // if user logged in, attach the userid and see if there is a file for it if (CommonRoutines.FileExists(CommonRoutines.basePath + "\\" + _userFileName)) { // set the filename to the user file name _fileName = _userFileName; } // get the full path for the config file _fileName = CommonRoutines.basePath + "\\" + _fileName; _originalFileName = _fileName; // clear the array configSettings.Clear(); //configRecords = CommonRoutines.ReadFile(_userFileName); configRecords = CommonRoutines.ReadFile(_fileName); // set (maybe again...) the filename so that if a new user comes in it will take the default config and then // when saved (upon exit) the new user config will be created. //_fileName = CommonRoutines.basePath + "\\" + _userFileName; _fileName = CommonRoutines.basePath + _userFileName; currentConfigFile = _fileName; for (int n = 0; n < configRecords.Count; n++) { _buffer = configRecords[n]; if (_buffer.Trim().Length > 0) { _numberOfLinesRead++; if (_buffer.ToUpper().IndexOf("<USERSETTINGS") >= 0) { _userSettings = true; } if (_buffer.ToUpper().IndexOf("</USERSETTINGS") >= 0) { _userSettings = false; } if (_userSettings) { if (_buffer.ToUpper().IndexOf("<SETTING") >= 0) { // get the key _setting = true; _offset = _buffer.ToUpper().IndexOf("NAME="); if (_offset > 0) { _offset += 5; _offset2 = _buffer.ToUpper().IndexOf(" ", _offset); if (_offset2 > 0) { _key = _buffer.Substring(_offset, (_offset2 - _offset)); // now remove any quotes _key = _key.Replace("\"", ""); } } } if (_buffer.ToUpper().IndexOf("</SETTING") >= 0) { _setting = false; // add to the config list udtConfigSettings configSetting = new udtConfigSettings(); configSetting.key = _key; configSetting.value = _value; configSetting.originalValue = _value; configSetting.source = "F"; configSettings.Add(configSetting); _key = ""; _value = ""; } if (_buffer.ToUpper().IndexOf("<VALUE") >= 0) { _offset = _buffer.IndexOf(">") + 1; _offset2 = _buffer.IndexOf("<", _offset); _value = _buffer.Substring(_offset, (_offset2 - _offset)); } } } } //LAP 20130916 - if config file cannot be found/read, return -1 to indicate error if (configRecords.Count < 1) { return configRecords.Count; } //LAP // see if the redirect setting is there... string _redirect = GetSetting("Redirect"); if (_redirect != "") { } // set the database connection DatabaseRoutines.BaseConnection = ConfigRoutines.GetSetting("BaseConnection"); DatabaseRoutines.BaseDBMS = ConfigRoutines.GetSetting("BaseDBMS"); // check for connectivity if (DatabaseRoutines.TestConnection()) { // load the rest of the configuration string _sql = "Select * From Config Where User_Id='" + CSA.currentUser + "' and Status='A' Order by Key_Name "; List<string[]> _rows = DatabaseRoutines.Select(_sql); if (_rows.Count > 1) { dbConfigSettingsFound = true; for (int n = 1; n < _rows.Count; n++) { // add to the config list udtConfigSettings _configSetting = new udtConfigSettings(); _configSetting.key = DatabaseRoutines.GetRowValue(_rows[0], _rows[n], "Key_Name"); _configSetting.value = DatabaseRoutines.GetRowValue(_rows[0], _rows[n], "Key_Value"); _configSetting.originalValue = _configSetting.value; _configSetting.source = "D"; configSettings.Add(_configSetting); // if a variable setting, add to the CSA properties if (_configSetting.key.ToUpper().IndexOf("VARIABLE_") == 0) { string _configKey = _configSetting.key.Substring(9); CSA.AddProperty("CONFIG",_configKey, _configSetting.value); } } } } else { // restore to previous configSettings = _configSettings; //LAP 20130916 - if base connection is invalid, return -1 to indicate error return configRecords.Count; //LAP } // is the original file name equal to the user file name? if (_originalFileName.ToUpper() != _fileName.ToUpper()) { // save to the new user file name WriteConfigFile(); } } catch (Exception ex) { CommonRoutines.DisplayErrorMessage("$E:" + moduleName + ".ReadConfigFile > " + ex.Message); } return configSettings.Count; }