winform中获取Properties窗口的值.

时间:2023-03-08 18:27:45
winform中获取Properties窗口的值.

我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Properties {
class Properties {
/// <summary>
/// 通过类型和对象获取对象键-值的字典集合.
/// </summary>
/// <param name="t"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static Dictionary<string, string> GetProperties(Type t, object obj) {
Dictionary<string, string> dic = new Dictionary<string, string>();
string k, v;
foreach (System.Reflection.PropertyInfo pInfo in t.GetProperties()) {
try {
//this.LayoutEngine = null;
object[] attibutes = pInfo.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), true);
if (attibutes.Length <= ) continue;
k = pInfo.Name;
v = pInfo.GetValue(obj, null) + "";
dic.Add(k, v);
}
catch (System.Exception ex) {
//todo:
}
} return dic;
} /// <summary>
/// 将GetProperties所得的键-值写入指定的文件中.
/// </summary>
/// <param name="dic"></param>
/// <param name="fullFileName"></param>
public static void SaveToFile(Dictionary<string, string> dic, string fullFileName) {
try {
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> kv in dic) {
sb.Append(kv.Key).Append(": ").AppendLine(kv.Value);
}
System.IO.File.WriteAllText(fullFileName, sb.ToString());
}
catch (System.Exception ex) {
throw new ArgumentException(ex.Message);
}
}
}
}