diff --git a/SunnyUI/Common/UIniConfig.cs b/SunnyUI/Common/UIniConfig.cs
index a8dddde9..50e14ec4 100644
--- a/SunnyUI/Common/UIniConfig.cs
+++ b/SunnyUI/Common/UIniConfig.cs
@@ -35,11 +35,9 @@ namespace Sunny.UI
///
/// INI 配置文件类
///
- /// 类型
- public class IniConfig : BaseConfig where TConfig : IniConfig, new()
+ /// 类型
+ public class IniConfig : BaseConfig where T : IniConfig, new()
{
- #region 加载
-
///
/// Ini文件编码格式
///
@@ -97,7 +95,7 @@ namespace Sunny.UI
if (filename.IsNullOrWhiteSpace())
{
- throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!");
+ throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
}
if (!File.Exists(filename))
@@ -155,7 +153,7 @@ namespace Sunny.UI
if (filename.IsNullOrWhiteSpace())
{
- throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!");
+ throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
}
ConcurrentDictionary idents = ConfigHelper.InitIdents(current);
@@ -169,7 +167,8 @@ namespace Sunny.UI
ConfigHelper.SaveConfigValue(Current, idents);
StringBuilder strs = new StringBuilder();
- strs.AppendLine(";");
+ strs.AppendLine(";");
+ strs.AppendLine(";");
strs.AppendLine("");
Dictionary> listidents = new Dictionary>();
foreach (var ident in idents.Values)
@@ -236,7 +235,198 @@ namespace Sunny.UI
MessageBox.Show("配置文件存储失败: " + filename + " ," + ex.Message);
}
}
+ }
- #endregion 加载
+ ///
+ /// INI 配置文件类
+ ///
+ /// 类型
+ public class IniUTF8Config : BaseConfig where T : IniUTF8Config, new()
+ {
+ [ConfigIgnore]
+ public object this[string property]
+ {
+ get
+ {
+ PropertyInfo info = Current.GetType().GetProperty(property);
+ if (info == null)
+ {
+ throw new ArgumentNullException("属性名称不存在:" + property);
+ }
+
+ return info.GetValue(this, null);
+ }
+ set
+ {
+ PropertyInfo info = Current.GetType().GetProperty(property);
+ if (info == null)
+ {
+ throw new ArgumentNullException("属性名称不存在:" + property);
+ }
+
+ try
+ {
+ Type propertyType = info.PropertyType;
+ info.SetValue(Current, Convert.ChangeType(value, propertyType), null);
+ }
+ catch (Exception)
+ {
+ throw new ArgumentException("属性值转换失败:" + property + ", " + value);
+ }
+ }
+ }
+
+ /// 加载指定配置文件
+ /// 文件名
+ /// 结果
+ public override bool Load(string filename)
+ {
+ if (filename.IsNullOrWhiteSpace())
+ {
+ filename = DirEx.CurrentDir() + ConfigFile;
+ }
+
+ if (filename.IsNullOrWhiteSpace())
+ {
+ throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
+ }
+
+ if (!File.Exists(filename))
+ {
+ return false;
+ }
+
+ try
+ {
+ ConcurrentDictionary idents = ConfigHelper.InitIdents(current);
+ foreach (var ident in idents.Values)
+ {
+ if (ident.Section.IsNullOrEmpty())
+ {
+ ident.Section = "Setup";
+ }
+ }
+
+ IniFile ini = new IniFile(filename, Encoding.UTF8);
+ foreach (var ident in idents.Values)
+ {
+ if (ident.IsList)
+ {
+ ident.Values.Clear();
+ NameValueCollection list = ini.GetSectionValues(ident.Section + "-" + ident.Key);
+ foreach (var pair in list)
+ {
+ ident.Values.Add(ini.Read(ident.Section + "-" + ident.Key, pair.ToString(), ""));
+ }
+ }
+ else
+ {
+ ident.Value = ini.Read(ident.Section, ident.Key, "");
+ }
+ }
+
+ ConfigHelper.LoadConfigValue(current, idents);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ return false;
+ }
+ }
+
+ /// 保存到配置文件中去
+ /// 文件名
+ public override void Save(string filename)
+ {
+ if (filename.IsNullOrWhiteSpace())
+ {
+ filename = DirEx.CurrentDir() + ConfigFile;
+ }
+
+ if (filename.IsNullOrWhiteSpace())
+ {
+ throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
+ }
+
+ ConcurrentDictionary idents = ConfigHelper.InitIdents(current);
+ foreach (var ident in idents.Values)
+ {
+ if (ident.Section.IsNullOrEmpty())
+ {
+ ident.Section = "Setup";
+ }
+ }
+
+ ConfigHelper.SaveConfigValue(Current, idents);
+ StringBuilder strs = new StringBuilder();
+ strs.AppendLine(";");
+ strs.AppendLine(";");
+ strs.AppendLine("");
+ Dictionary> listidents = new Dictionary>();
+ foreach (var ident in idents.Values)
+ {
+ string section = ident.IsList ? ident.Section + "-" + ident.Key : ident.Section;
+
+ if (!listidents.ContainsKey(section))
+ {
+ listidents.Add(section, new List());
+ }
+
+ listidents[section].Add(ident);
+ }
+
+ foreach (var values in listidents)
+ {
+ strs.AppendLine("[" + values.Key + "]");
+
+ SortedList slist = new SortedList();
+ foreach (var ident in values.Value)
+ {
+ slist.Add(ident.Index, ident);
+ }
+
+ foreach (var ident in slist.Values)
+ {
+ if (!ident.Description.IsNullOrEmpty())
+ {
+ strs.AppendLine(";");
+ }
+
+ if (ident.IsList)
+ {
+ for (int i = 0; i < ident.Values.Count; i++)
+ {
+ strs.AppendLine("Value" + i + "=" + ident.Values[i]);
+ }
+ }
+ else
+ {
+ strs.AppendLine(ident.Key + "=" + ident.Value);
+ }
+ }
+
+ strs.AppendLine("");
+ }
+
+ listidents.Clear();
+ DirEx.CreateDir(Path.GetDirectoryName(filename));
+ string filetmp = filename + "." + RandomEx.RandomPureChar(3);
+ File.Delete(filetmp);
+ StreamWriter sw = new StreamWriter(filetmp, false, Encoding.UTF8);
+ sw.WriteLine(strs.ToString());
+ sw.Flush();
+ sw.Dispose();
+
+ try
+ {
+ File.Delete(filename);
+ File.Move(filetmp, filename);
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("配置文件存储失败: " + filename + " ," + ex.Message);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/SunnyUI/Common/UIniFile.cs b/SunnyUI/Common/UIniFile.cs
index bc019465..d192f622 100644
--- a/SunnyUI/Common/UIniFile.cs
+++ b/SunnyUI/Common/UIniFile.cs
@@ -78,7 +78,7 @@ namespace Sunny.UI
//文件不存在,建立文件
using (StreamWriter sw = new StreamWriter(fileName, false, IniEncoding))
{
- sw.WriteLine(";");
+ sw.WriteLine(";");
sw.WriteLine("");
}
}