How to save classic Delphi string to disk (and read them back)?

前端 未结 4 595
走了就别回头了
走了就别回头了 2021-01-07 16:03

I want to achieve a very very basic task in Delphi: to save a string to disk and load it back. It seems trivial but I had problems doing this TWICE since I upgraded to IOUti

4条回答
  •  悲哀的现实
    2021-01-07 16:15

    The Inifiles unit should support unicode. At least according to this answer: How do I read a UTF8 encoded INI file?

    Inifiles are quite commonly used to store strings, integers, booleans and even stringlists.

        procedure TConfig.ReadValues();
        var
            appINI: TIniFile;
        begin
            appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
    
            try
                FMainScreen_Top := appINI.ReadInteger('Options', 'MainScreen_Top', -1);
                FMainScreen_Left := appINI.ReadInteger('Options', 'MainScreen_Left', -1);
                FUserName := appINI.ReadString('Login', 'UserName', '');
                FDevMode := appINI.ReadBool('Globals', 'DevMode', False);
            finally
                appINI.Free;
            end;
        end;
    
        procedure TConfig.WriteValues(OnlyWriteAnalyzer: Boolean);
        var
            appINI: TIniFile;
        begin
            appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
    
            try
                appINI.WriteInteger('Options', 'MainScreen_Top', FMainScreen_Top);
                appINI.WriteInteger('Options', 'MainScreen_Left', FMainScreen_Left);
                appINI.WriteString('Login', 'UserName', FUserName);
                appINI.WriteBool('Globals', 'DevMode', FDevMode);
            finally
                appINI.Free;
            end;
        end;
    

    Also see the embarcadero documentation on inifiles: http://docwiki.embarcadero.com/Libraries/Seattle/en/System.IniFiles.TIniFile

提交回复
热议问题