public void Read() { if (File.Exists(GetFileName())) { foreach (PropertyInfo propertyInfo in this.GetType().GetProperties()) { if (propertyInfo.CanWrite) { StringBuilder strValue1 = new StringBuilder(1024); string strValue; string sectionName = "System"; INIAttribute atttribute = propertyInfo.GetCustomAttribute(typeof(INIAttribute)) as INIAttribute; if (atttribute != null && !string.IsNullOrEmpty(atttribute.SectionName)) { sectionName = atttribute.SectionName; } GetPrivateProfileString(sectionName, propertyInfo.Name, "0", strValue1); strValue = strValue1.ToString(); object objN = propertyInfo.GetValue(this, null); if (objN is int) { propertyInfo.SetValue(this, Convert.ToInt32(strValue), null); } else if (objN is double) { propertyInfo.SetValue(this, Convert.ToDouble(strValue), null); } else if (objN is string) { propertyInfo.SetValue(this, strValue, null); } else if (objN is bool) { propertyInfo.SetValue(this, bool.TrueString == strValue, null); } else { this.SetUserDefineValueAction?.Invoke(propertyInfo, strValue); } } } } }
public void Save() { if (!File.Exists(GetFileName())) { PathHelper.CreatePath(GetFileName()); File.CreateText(GetFileName()); } foreach (PropertyInfo propertyInfo in this.GetType().GetProperties()) { if (propertyInfo.CanWrite) { string sectionName = "System"; INIAttribute atttribute = propertyInfo.GetCustomAttribute(typeof(INIAttribute)) as INIAttribute; if (atttribute != null && !string.IsNullOrEmpty(atttribute.SectionName)) { sectionName = atttribute.SectionName; } this.WritePrivateProfileString(sectionName, propertyInfo.Name, propertyInfo.GetValue(this, null).ToString()); } } }