* IniUTF8Config: 增加一种默认编码为UTF8的Ini配置文件读写类
This commit is contained in:
parent
1c6c9fe34f
commit
23ae9da234
@ -35,11 +35,9 @@ namespace Sunny.UI
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// INI 配置文件类
|
/// INI 配置文件类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TConfig">类型</typeparam>
|
/// <typeparam name="T">类型</typeparam>
|
||||||
public class IniConfig<TConfig> : BaseConfig<TConfig> where TConfig : IniConfig<TConfig>, new()
|
public class IniConfig<T> : BaseConfig<T> where T : IniConfig<T>, new()
|
||||||
{
|
{
|
||||||
#region 加载
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ini文件编码格式
|
/// Ini文件编码格式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -97,7 +95,7 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
if (filename.IsNullOrWhiteSpace())
|
if (filename.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!");
|
throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename))
|
||||||
@ -155,7 +153,7 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
if (filename.IsNullOrWhiteSpace())
|
if (filename.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!");
|
throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
|
||||||
}
|
}
|
||||||
|
|
||||||
ConcurrentDictionary<string, Ident> idents = ConfigHelper.InitIdents(current);
|
ConcurrentDictionary<string, Ident> idents = ConfigHelper.InitIdents(current);
|
||||||
@ -169,7 +167,8 @@ namespace Sunny.UI
|
|||||||
|
|
||||||
ConfigHelper.SaveConfigValue(Current, idents);
|
ConfigHelper.SaveConfigValue(Current, idents);
|
||||||
StringBuilder strs = new StringBuilder();
|
StringBuilder strs = new StringBuilder();
|
||||||
strs.AppendLine(";<?ini version=\"" + UIGlobal.Version + "\" encoding=\"" + IniEncoding.BodyName + "\"?>");
|
strs.AppendLine(";<?Ini Version=\"" + UIGlobal.Version + "\" Encoding=\"" + IniEncoding.BodyName + "\"?>");
|
||||||
|
strs.AppendLine(";<?Update Time=\"" + DateTime.Now.DateTimeString() + "\"?>");
|
||||||
strs.AppendLine("");
|
strs.AppendLine("");
|
||||||
Dictionary<string, List<Ident>> listidents = new Dictionary<string, List<Ident>>();
|
Dictionary<string, List<Ident>> listidents = new Dictionary<string, List<Ident>>();
|
||||||
foreach (var ident in idents.Values)
|
foreach (var ident in idents.Values)
|
||||||
@ -236,7 +235,198 @@ namespace Sunny.UI
|
|||||||
MessageBox.Show("配置文件存储失败: " + filename + " ," + ex.Message);
|
MessageBox.Show("配置文件存储失败: " + filename + " ," + ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion 加载
|
/// <summary>
|
||||||
|
/// INI 配置文件类
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">类型</typeparam>
|
||||||
|
public class IniUTF8Config<T> : BaseConfig<T> where T : IniUTF8Config<T>, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>加载指定配置文件</summary>
|
||||||
|
/// <param name="filename">文件名</param>
|
||||||
|
/// <returns>结果</returns>
|
||||||
|
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<string, Ident> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>保存到配置文件中去</summary>
|
||||||
|
/// <param name="filename">文件名</param>
|
||||||
|
public override void Save(string filename)
|
||||||
|
{
|
||||||
|
if (filename.IsNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
filename = DirEx.CurrentDir() + ConfigFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filename.IsNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
throw new ApplicationException($"未指定{typeof(T).Name}的配置文件路径!");
|
||||||
|
}
|
||||||
|
|
||||||
|
ConcurrentDictionary<string, Ident> 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(";<?Ini Version=\"" + UIGlobal.Version + "\" Encoding=\"" + Encoding.UTF8.BodyName + "\"?>");
|
||||||
|
strs.AppendLine(";<?Update Time=\"" + DateTime.Now.DateTimeString() + "\"?>");
|
||||||
|
strs.AppendLine("");
|
||||||
|
Dictionary<string, List<Ident>> listidents = new Dictionary<string, List<Ident>>();
|
||||||
|
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<Ident>());
|
||||||
|
}
|
||||||
|
|
||||||
|
listidents[section].Add(ident);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var values in listidents)
|
||||||
|
{
|
||||||
|
strs.AppendLine("[" + values.Key + "]");
|
||||||
|
|
||||||
|
SortedList<int, Ident> slist = new SortedList<int, Ident>();
|
||||||
|
foreach (var ident in values.Value)
|
||||||
|
{
|
||||||
|
slist.Add(ident.Index, ident);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var ident in slist.Values)
|
||||||
|
{
|
||||||
|
if (!ident.Description.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
strs.AppendLine(";<!--" + ident.Description + "-->");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -78,7 +78,7 @@ namespace Sunny.UI
|
|||||||
//文件不存在,建立文件
|
//文件不存在,建立文件
|
||||||
using (StreamWriter sw = new StreamWriter(fileName, false, IniEncoding))
|
using (StreamWriter sw = new StreamWriter(fileName, false, IniEncoding))
|
||||||
{
|
{
|
||||||
sw.WriteLine(";<?ini version=\"" + UIGlobal.Version + "\" encoding=\"" + IniEncoding.BodyName + "\"?>");
|
sw.WriteLine(";<?Ini Version=\"" + UIGlobal.Version + "\" Encoding=\"" + Encoding.UTF8.BodyName + "\"?>");
|
||||||
sw.WriteLine("");
|
sw.WriteLine("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user