顯示廣告
隱藏 ✕
看板 P_qman
作者 cuteman0725(訪客)
標題 [備忘] Delphi 封裝 INI
時間 2010年02月01日 Mon. AM 12:00:57


http://hi.baidu.com/tintinsoft/blog/item/08b9cb17ae216711972b438b.html

uses IniFiles;

//簡單數據類型
TSimpleType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean);

   /// <summary>
    /// 讀Ini文件的函數
    /// </summary>
    /// <param name="FileName">Ini文件名</param>
    /// <param name="Section">節點</param>
    /// <param name="Name">字段名</param>
    /// <param name="SimpleType">簡單數據類型</param>
    /// <param name="DefaultValue">默認值</param>
    /// <returns>返回變體類型</returns>

function ReadIniValue(const FileName, Section, Name: string;
      SimpleType: TSimpleType; DefaultValue: Variant): Variant;

begin

   with TIniFile.Create(FileName) do
try
    if SimpleType = stString then
      Result := ReadString(Section, Name, DefaultValue)
    else if SimpleType = stInt then
      Result := ReadInteger(Section, Name, DefaultValue)
    else if SimpleType = stFloat then
      Result := ReadFloat(Section, Name, DefaultValue)
    else if SimpleType = stDateTime then
      Result := ReadDateTime(Section, Name, DefaultValue)
    else if SimpleType = stDate then
      Result := ReadDate(Section, Name, DefaultValue)
    else if SimpleType = stTime then
      Result := ReadTime(Section, Name, DefaultValue)
    else if SimpleType = stBoolean then
      Result := ReadBool(Section, Name, DefaultValue);
finally
    Free;
end;

    /// <summary>
    /// 寫INI文件的函數
    /// </summary>
    /// <param name="FileName">ini文件名</param>
    /// <param name="Section">節點名</param>
    /// <param name="Name">字段名</param>
    /// <param name="Value">字段值</param>
    /// <param name="SimpleType">簡單類型</param>  

procedure WriteIniValue(const FileName, Section, Name: string;
      Value: Variant; SimpleType: TSimpleType);
begin
with TIniFile.Create(FileName) do
try
    if SimpleType = stString then
      WriteString(Section, Name, VarToStr(Value))
    else if SimpleType = stInt then
      WriteInteger(Section, Name, Value)
    else if SimpleType = stFloat then
      WriteFloat(Section, Name, Value)
    else if SimpleType = stDateTime then
      WriteDateTime(Section, Name, VarToDateTime(Value))
    else if SimpleType = stDate then
      WriteDate(Section, Name, VarToDateTime(Value))
    else if SimpleType = stTime then
      WriteTime(Section, Name, VarToDateTime(Value))
    else if SimpleType = stBoolean then
      WriteBool(Section, Name, Value);
finally
    Free;
end;

end;

調用範例:

WriteIniValue('c:\中國爛鞋.ini','鍋家隊','豬教練','郭十二',stString);

WriteIniValue('c:\中國爛鞋.ini','鍋家隊','平均罰球',1,stInt);

ReadIniValue('c:\中國爛鞋.ini','鍋家隊','豬教練',stString,'郭12');

ReadIniValue('c:\中國爛鞋.ini','鍋家隊','平均罰球',stInt,-1);

--
※ 來源: DISP BBS (http://disp.twbbs.org)
※ 作者: cuteman0725  來自: 112.104.95.159  時間: 2010-02-01 00:00:57
※ 看板: P_qman 文章推薦值: 0 目前人氣: 0 累積人氣: 213 
分享網址: 複製 已複製
e)編輯 d)刪除 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇