INI建立 读取 存入

---恢复内容开始---

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Runtime.InteropServices;
  7 
  8 namespace INI
  9 {
 10     public class TINI : IDisposable
 11     {
 12         [DllImport("kernel32")]
 13         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 14         [DllImport("kernel32")]
 15         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 16 
 17         private bool bDisposed = false;
 18         private string _FilePath = string.Empty;
 19         public string FilePath
 20         {
 21             get
 22             {
 23                 if (_FilePath == null)
 24                     return string.Empty;
 25                 else
 26                     return _FilePath;
 27             }
 28 
 29             set
 30             {
 31                 if (_FilePath != value)
 32                     _FilePath = value;
 33             }
 34 
 35         }
 36    
 37         public TINI(string path)
 38         {
 39             _FilePath = path;
 40         }
 41 
 42         ~TINI()
 43         {
 44             Dispose(false);
 45         }
 46 
 47         public void Dispose()
 48         {
 49 
 50             Dispose(true);
 51             GC.SuppressFinalize(this); 
 52         }
 53  
 54         protected virtual void Dispose(bool IsDisposing)
 55         {
 56             if (bDisposed)
 57             {
 58                 return;
 59             }
 60 
 61             if (IsDisposing)
 62             {
 63                 //补充
 64                 //这里释放实体 IDisposable 的物件
 65                 //ex: DataSet DS = new DataSet(); 
 66                 //可在这里 使用 DS.Dispose(); 
 67                 //或是 DS = null; 
 68                 //或是释放自定义的物件。  
 69             }
 70             bDisposed = true;
 71 
 72         }
 73 
 74         /// <summary> 
 75         /// 設定 KeyValue 值。 
 76         /// </summary> 
 77         /// <param name="IN_Section">Section。</param> 
 78         /// <param name="IN_Key">Key。</param> 
 79         /// <param name="IN_Value">Value。</param> 
 80         public void setKeyValue(string IN_Section, string IN_Key, string IN_Value)
 81         {
 82             WritePrivateProfileString(IN_Section, IN_Key, IN_Value, this._FilePath);
 83 
 84         }
 85 
 86         public void setKeyValue(string IN_Section, string IN_Key, int IN_Value)
 87         {
 88             WritePrivateProfileString(IN_Section, IN_Key, Convert.ToString(IN_Value), this._FilePath);
 89 
 90         }
 91 
 92         public void setKeyValue(string IN_Section, string IN_Key, double IN_Value)
 93         {
 94             WritePrivateProfileString(IN_Section, IN_Key, Convert.ToString(IN_Value), this._FilePath);
 95 
 96         }
 97 
 98         /// <summary> 
 99         /// 取得 Key 相對的 Value 值。 
100         /// </summary> 
101         /// <param name="IN_Section">Section。</param> 
102         /// <param name="IN_Key">Key。</param>         
103         public string getKeyValue(string IN_Section, string IN_Key)
104         {
105             StringBuilder temp = new StringBuilder(255);
106             int i = GetPrivateProfileString(IN_Section, IN_Key, "", temp, 255, this._FilePath);
107             return temp.ToString();
108         }
109 
110         /// <summary> 
111         /// 取得 Key 相對的 Value 值,若沒有則使用預設值(DefaultValue)。 
112         /// </summary> 
113         /// <param name="Section">Section。</param> 
114         /// <param name="Key">Key。</param> 
115         /// <param name="DefaultValue">DefaultValue。</param>         
116         public string getKeyValue(string Section, string Key, string DefaultValue)
117         {
118             StringBuilder sbResult = null;
119             try
120             {
121                 sbResult = new StringBuilder(255);
122                 GetPrivateProfileString(Section, Key, "", sbResult, 255, this._FilePath);
123                 return (sbResult.Length > 0) ? sbResult.ToString() : DefaultValue;
124             }
125             catch
126             {
127                 return string.Empty;
128             }
129 
130         }
131 
132     }
133 
134 }

 

---恢复内容结束---

posted @ 2015-06-04 15:10  曉軒  阅读(163)  评论(0编辑  收藏  举报