INI 檔讀寫

1 篇文章 / 0 new
author
INI 檔讀寫
.Net 內的API 已沒有內建對INI檔的操作, 主因是 .Net 以 xml 檔為主, 若仍要使用 ini 檔則除自己寫 code 外, 另一個方式就是透過使用 windows 系統 dll 的方法分別為GetPrivateProfileStringWritePrivateProfileString 來進行操作, 程式中引入方法如下
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
► 參數說明
GetPrivateProfileString 參數:
lpAppName:section名稱
lpKeyName:key名稱
lpDefault:如果沒有被找到時的預設值
lpReturnedString:返回結果的值
nSize:lpReturnedString的字元長度
lpFileName:INI檔名

WritePrivateProfileString 參數:
lpAppName:section名稱
lpKeyName:key名稱
lpString:寫入值與
lpFileName:INI檔名
Free Web Hosting