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 }

 

转载于:https://www.cnblogs.com/listened/p/4042117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值