* UIniConfig: 增加按属性名称进行读写

This commit is contained in:
Sunny 2024-02-27 22:45:01 +08:00
parent 00c7147491
commit ff40c3b39d

View File

@ -18,6 +18,7 @@
* *
* 2020-01-01: V2.2.0 * 2020-01-01: V2.2.0
* 2022-11-01: V3.2.6 Load传入 * 2022-11-01: V3.2.6 Load传入
* 2024-02-27: V3.6.3
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -25,7 +26,9 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Reflection;
using System.Text; using System.Text;
using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
@ -43,6 +46,39 @@ namespace Sunny.UI
[ConfigIgnore] [ConfigIgnore]
public Encoding IniEncoding { get; private set; } = Encoding.Default; public Encoding IniEncoding { get; private set; } = Encoding.Default;
[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 bool Load(string fileName, Encoding encoding) public bool Load(string fileName, Encoding encoding)
{ {
IniEncoding = encoding; IniEncoding = encoding;
@ -188,10 +224,17 @@ namespace Sunny.UI
StreamWriter sw = new StreamWriter(filetmp, false, IniEncoding); StreamWriter sw = new StreamWriter(filetmp, false, IniEncoding);
sw.WriteLine(strs.ToString()); sw.WriteLine(strs.ToString());
sw.Flush(); sw.Flush();
sw.Close();
sw.Dispose(); sw.Dispose();
File.Delete(filename);
File.Move(filetmp, filename); try
{
File.Delete(filename);
File.Move(filetmp, filename);
}
catch (Exception ex)
{
MessageBox.Show("配置文件存储失败: " + filename + " ," + ex.Message);
}
} }
#endregion #endregion