要利用.INI文件做程序有关数据的存储工作,就需要能读和写.INI文件,所以列了如下方法给大家参考:
从.INI文件中获取字符串
var
strResult:pchar;
begin
GetPrivateProfileString(
'windows', // []中标题的名字
'NullPort', // =号前的名字
'NIL', // 如果没有找到字符串时,返回的默认值
strResult, //存放取得字符
100, //取得字符的允许最大长度
'c:\forwin95\win.ini' // 调用的文件名
);
edit1.text:=strResult; //显示取得字符串
从.INI文件中获取整数
edit1.text:=inttostr(GetPrivateProfileInt(
'intl', // []中标题的名字
'iCountry', // =号前的名字
0,// 如果没有找到整数时,返回的默认值
'c:\forwin95\win.ini' // 调用的文件名
));
向.INI文件写入字符串
WritePrivateProfileString(
'windows', // []中标题的名字
'load', // 要写入“=”号前的字符串
'accca', //要写入的数据
'c:\forwin95\win.ini' // 调用的文件名
);
向.INI文件写入整数
WritePrivateProfileSection(
'windows', // []中标题的名字
'read=100', // 要写入的数据
'c:\forwin95\win.ini' // 调用的文件名
);  

解决方案 »

  1.   

    用TINIFILE控件较简单些://读取设置参数
    procedure TMainForm.LoadParameters;
    var
        TheIni:TIniFile;
    begin
        TheIni:=TIniFile.Create(Sysdir+'System.Ini');
        try
            with TheIni do
            begin
              AutoDelete:=ReadBool('System','AutoDelete',False);
              AutoDeleteTime:=ReadInteger('System','AutoDeleteTime',0);
              NumberItems:=ReadInteger('System','NumberItems',0);
            end;
        finally
            TheIni.Free;
        end;
    end;//保存设置参数
    Procedure TMainForm.SaveParameters;
    var
        TheIni:TIniFile;
    begin
        TheIni:=TIniFile.Create(Sysdir+'System.Ini');
        try
            with TheIni do
            begin
              WriteBool('System','AutoDelete',AutoDelete);
              WriteInteger('System','AutoDeleteTime',AutoDeleteTime);
              WriteInteger('System','NumberItems',NumberItems);
            end;
        finally
            TheIni.Free;
        end;
    end;
      

  2.   

    还有,在USES语句要加 INIFILES 单元。
    Uses IniFiles;
      

  3.   

    同意黑子,不过那不是控件,是一个类。用INI类很方便的,简单的要命!看看帮助就知道了。
      

  4.   

    var
      ini:Tinifile;
      name:string;
    begin
      name := ExtractFilePath(Application.ExeName)+ExtrcatFileExt(ExtractFileName(Application.ExeName),'.ini'); //通过执行文件获得将创建的INI的文件名
      ini := TiniCreate(name)
      ini.writeString('system','user',user.text);
      sysdis := ini.readString('system','sysdir','sys');
      ....................
    多看帮助
      

  5.   

    哦!我写错了:ExtrcatFileExt应该为ChangeFileExt!
    实在不好意思!
      

  6.   

    WritePrivateProfileString
    WritePrivateProfileInt
    WritePrivateProfileSection
    等等可以写任何文本方式存储的文件,文件后缀可以是.txt、.dat等,但写后文件结构与.ini文件结构类似
    GetPrivateProfileString
    GetPrivateProfileInt等只能读.ini文件结构的文件,文件后缀可以是.txt、.dat等
    详细可见msdn
      

  7.   

    我用zwd() 和jimmey(小虫)的方法,问题已经解决了,多谢!!!
      

  8.   

    我还想问一个问题,因为我是用treeview.selected.AddObject(Node: TTreeNode; const S: string; Ptr: Pointer)来输入数据的。里面有指针。
    请问用什么方法能连同指针一起保存到一个文件里{不是指.ini文件},并且下一次初始化时,所有指针都能再次被读出来?然后仍旧能够接着上一次保存的数据继续添加新数据?
      

  9.   

    对于要保存的每一个指针,自动生成一个序号(循环变量就是了),用序号作为INI项目的名字。而指针就可以转换为INTEGER然后保存。但如果是对象指针,它是64位的,处理上就有问题了。
    说实在的,保存指针有什么用?下次运行就全变了。