winform中获取Properties窗口的值.
我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Properties { 7 class Properties { 8 /// <summary> 9 /// 通过类型和对象获取对象键-值的字典集合. 10 /// </summary> 11 /// <param name="t"></param> 12 /// <param name="obj"></param> 13 /// <returns></returns> 14 public static Dictionary<string, string> GetProperties(Type t, object obj) { 15 Dictionary<string, string> dic = new Dictionary<string, string>(); 16 string k, v; 17 foreach (System.Reflection.PropertyInfo pInfo in t.GetProperties()) { 18 try { 19 //this.LayoutEngine = null; 20 object[] attibutes = pInfo.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), true); 21 if (attibutes.Length <= 0) continue; 22 k = pInfo.Name; 23 v = pInfo.GetValue(obj, null) + ""; 24 dic.Add(k, v); 25 } 26 catch (System.Exception ex) { 27 //todo: 28 } 29 } 30 31 return dic; 32 } 33 34 /// <summary> 35 /// 将GetProperties所得的键-值写入指定的文件中. 36 /// </summary> 37 /// <param name="dic"></param> 38 /// <param name="fullFileName"></param> 39 public static void SaveToFile(Dictionary<string, string> dic, string fullFileName) { 40 try { 41 StringBuilder sb = new StringBuilder(); 42 foreach (KeyValuePair<string, string> kv in dic) { 43 sb.Append(kv.Key).Append(": ").AppendLine(kv.Value); 44 } 45 System.IO.File.WriteAllText(fullFileName, sb.ToString()); 46 } 47 catch (System.Exception ex) { 48 throw new ArgumentException(ex.Message); 49 } 50 } 51 } 52 }