1、ini文件是什麼? 見百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin 2、C#語言實現ini文件的讀寫操作 此博文為原創,轉載請註明出處!!!!! ...
1、ini文件是什麼?
見百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin
2、C#語言實現ini文件的讀寫操作
1 /// <summary> 2 /// 配置文件 .ini操作類 3 /// </summary> 4 public class IniFileUtils 5 { 6 /// <summary> 7 /// 寫入INI文件 8 /// </summary> 9 /// <param name="section">節點名稱[如TypeName]</param> 10 /// <param name="key">鍵</param> 11 /// <param name="val">值</param> 12 /// <param name="filepath">文件路徑</param> 13 /// <returns></returns> 14 [DllImport("kernel32")] 15 private static extern long WritePrivateProfileString(string section, string key, string val, string filepath); 16 /// <summary> 17 /// 讀取INI文件 18 /// </summary> 19 /// <param name="section">節點名稱</param> 20 /// <param name="key">鍵</param> 21 /// <param name="def">值</param> 22 /// <param name="retval">stringbulider對象</param> 23 /// <param name="size">位元組大小</param> 24 /// <param name="filePath">文件路徑</param> 25 /// <returns></returns> 26 [DllImport("kernel32")] 27 private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath); 28 29 /// <summary> 30 /// 寫入或更新.ini配置文件屬性值 31 /// </summary> 32 /// <param name="section">區域(節點)</param> 33 /// <param name="key">key鍵屬性名稱</param> 34 /// <param name="value">key鍵對應屬性值param> 35 /// <param name="path">.ini文件所在路徑</param> 36 public static void WriteContentValue(string section, string key, string value, string path) 37 { 38 //判斷文件是或否存在 39 if (File.Exists(path)) 40 { 41 WritePrivateProfileString(section, key, value, path); 42 } 43 } 44 45 /// <summary> 46 /// 讀取.ini配置文件屬性值 47 /// </summary> 48 /// <param name="Section">區域(節點)</param> 49 /// <param name="key">key鍵屬性名稱</param> 50 /// <param name="path">.ini文件所在路徑</param> 51 /// <returns></returns> 52 public static string ReadContentValue(string Section, string key, string path) 53 { 54 StringBuilder temp = new StringBuilder(1024); 55 //判斷文件是或否存在 56 if (File.Exists(path)) 57 { 58 GetPrivateProfileString(Section, key, "", temp, 1024, path); 59 } 60 return temp.ToString(); 61 62 } 63 }
此博文為原創,轉載請註明出處!!!!!