void cmdSave_Click(object sender, EventArgs e) { if (!Dialog.ValidateIsNotEmpty(txtHostname)) return; if (!Dialog.ValidateIsNotEmpty(txtPassword)) return; if (txtHostname.Text.Trim().ToUpper() != _OriginalHostname) { using (IniFile Ini = new IniFile(Config.Default.FileName)) { Ini.EraseSection(_OriginalHostname); Ini.Save(); } } HostConfig HC = new HostConfig(txtHostname.Text.Trim()); HC.LastUpdateDate = DateTime.MinValue; HC.Password = txtPassword.SecureText.GetSecureText(); HC.Provider = ProviderName.DtDNS; HC.Username = HC.Hostname; if (HC.Disabled) { // Saving changes should reset the disabled state and last update date, so a new update can be attempted right away HC.Disabled = false; } HC.Save(); DialogResult = DialogResult.OK; }
/// <summary> /// Advanced delete method that allows you to specify the section to delete /// </summary> /// <param name="sectionName">The section to delete within the INI</param> protected void Delete(string sectionName) { // Store the section name SectionName = sectionName; // Load the application ini using (IniFile Ini = new IniFile(FileName, IniPassword)) { // Check if the desired section exists if (Ini.SectionExists(sectionName)) { // Yep, so delete it Ini.EraseSection(sectionName); Ini.Save(); } } }
/// <summary> /// Advanced save method that allows you to specify the section to save to /// </summary> /// <param name="sectionName">The section to save within the INI</param> protected void Save(string sectionName) { // Load the Ini IniFile Ini = new IniFile(FileName, IniPassword); // Loop through each field in the inherited class and write the value to the Ini PropertyInfo[] Properties = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); foreach (PropertyInfo Property in Properties) { // Ensure we only look at read+write properties (read only helper properties should not be loaded from/saved to an ini) if ((Property.CanRead) && (Property.CanWrite)) { switch (Property.PropertyType.Name) { case "Boolean": case "Byte": case "Char": case "DateTime": case "Decimal": case "Double": case "Int16": case "Int32": case "Int64": case "SByte": case "Single": case "String": case "UInt16": case "UInt32": case "UInt64": // All the built-in types can be saved via Ini.WriteString() Ini.WriteString(sectionName, Property.Name, Property.GetValue(this, null).ToString()); break; case "Boolean[]": case "Byte[]": case "Char[]": case "DateTime[]": case "Decimal[]": case "Double[]": case "Int16[]": case "Int32[]": case "Int64[]": case "SByte[]": case "Single[]": case "String[]": case "UInt16[]": case "UInt32[]": case "UInt64[]": Ini.WriteString(sectionName, Property.Name, (IList)Property.GetValue(this, null)); break; case "RMSecureString": // RMSecureString should be saved via Ini.WriteString() either protected or encrypted if (RMSecureStringPassword.Length == 0) { // No password means protected Ini.WriteString(sectionName, Property.Name, ((RMSecureString)Property.GetValue(this, null)).GetProtectedString(RMSecureStringPassword)); } else { // Password means encrypted Ini.WriteString(sectionName, Property.Name, ((RMSecureString)Property.GetValue(this, null)).GetEncryptedString(RMSecureStringPassword)); } break; case "StringDictionary": StringDictionary SD = (StringDictionary)Property.GetValue(this, null); foreach (DictionaryEntry DE in SD) { Ini.WriteString(sectionName, Property.Name + "_" + DE.Key.ToString(), DE.Value.ToString()); } break; default: // Check for enum, which we can save the string representation of if (Property.PropertyType.BaseType.Name == "Array") { Ini.WriteString(sectionName, Property.Name, (IList)Property.GetValue(this, null)); } else if (Property.PropertyType.BaseType.Name == "Enum") { Ini.WriteString(sectionName, Property.Name, Property.GetValue(this, null).ToString()); } break; } } } // Update the INI files Ini.Save(); }