본문 바로가기
WORK/Sotfware

ini file Help

by KANG Stroy 2009. 8. 26.
728x90
728x90

This example reads the myapp.ini file and displays on the form the status of your auto save options.
Before you run this example, you must include IniFiles.hpp in your unit.

void __fastcall TForm1::FormCreate(TObject *Sender)

{
  TIniFile *pIniFile = new TIniFile("c:\\MyApp\\bin\\MyApp.ini");
  if (pIniFile->ReadBool("AutoLoad", "FormProperties", false) == true)
  {
    Visible = pIniFile->ReadBool("FormOptions", "Visible", true);
    Color = (Graphics::TColor)pIniFile->ReadInteger("FormOptions", "Color", clWindow);
    Caption = pIniFile->ReadString("FormOptions", "Caption", "Main");
  }
  delete pIniFile;
}

======================================================================================================

The ini file format is still popular, many configuration files (such as the DSK Desktop settings file) are in this format. This format is especially useful in cross-platform applications, where you can뭪 always count on a system Registry for storing configuration information. BaseCLX provides two classes, TIniFile and TMemIniFile, to make reading and writing ini files very easy.
On Linux, TMemIniFile and TIniFile are identical. On Windows, TIniFile works directly with the ini file on disk while TMemIniFile buffers all changes in memory and does not write them to disk until you call the UpdateFile method.

When you instantiate the TIniFile or TMemIniFile object, you pass the name of the ini file as a parameter to the constructor. If the file does not exist, it is automatically created. You are then free to read values using the various read methods, such as ReadString, ReadDate, ReadInteger, or ReadBool. Alternatively, if you want to read an entire section of the ini file, you can use the ReadSection method. Similarly, you can write values using methods such as WriteBool, WriteInteger, WriteDate, or WriteString.

Following is an example of reading configuration information from an ini file in a form's constructor and writing values in the OnClose event handler.

__fastcall TForm1::TForm1(TComponent *Owner) : TForm(Owner)

{
   TIniFile *ini;
   ini = new TIniFile( ChangeFileExt( Application->ExeName, ".INI" ) );
   Top     =  ini->ReadInteger( "Form", "Top", 100 );
   Left    =  ini->ReadInteger( "Form", "Left", 100 );
   Caption =  ini->ReadString( "Form", "Caption",
                               "Default Caption" );
   ini->ReadBool( "Form", "InitMax", false ) ?
         WindowState = wsMaximized :
         WindowState = wsNormal;
   delete ini;
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)

{
   TIniFile *ini;
   ini = new TIniFile(ChangeFileExt( Application->ExeName, ".INI" ) );
   ini->WriteInteger( "Form", "Top", Top );
   ini->WriteInteger( "Form", "Left", Left );
   ini->WriteString ( "Form", "Caption", Caption );
   ini->WriteBool   ( "Form", "InitMax",
                       WindowState == wsMaximized );
   delete ini;
}

Each of the Read routines takes three parameters. The first parameter identifies the section of the ini file. The second parameter identifies the value you want to read, and the third is a default value in case the section or value doesn't exist in the ini file. Just as the Read methods gracefully handle the case when a section or value does not exist, the Write routines create the section and/or value if they do not exist. The example code creates an ini file the first time it is run that looks like this:

[Form]

Top=185
Left=280
Caption=Default Caption
InitMax=0

On subsequent execution of this application, the ini values are read in when the form is created and written back out in the OnClose event.

728x90

'WORK > Sotfware' 카테고리의 다른 글

컴퓨터 시간 가져오기  (0) 2010.01.19
TIniFile 볼란드 헬프에서~!  (0) 2009.08.27
ini file로 저장,읽기  (0) 2009.08.26
BCB 가속기 [Compiler/Code insight 가속기 2.7 정식 버전.]  (0) 2009.08.26
볼란드 excel 제어  (0) 2009.08.25

댓글